在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 定时器例子(定时关机/重启/注销)

C# 定时器例子(定时关机/重启/注销)

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:3.26M
  • 下载次数:42
  • 浏览次数:311
  • 发布时间:2018-08-28
  • 实例类别:C#语言基础
  • 发 布 人:tqx01
  • 文件格式:.zip
  • 所需积分:2
 相关标签: 定时器 定时

实例介绍

【实例简介】

【实例截图】

from clipboard

【核心代码】


using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace Windowsclose
{
	public class 定时关机 : Form
	{
		private IContainer components = null;

		private Label label1;

		private Label label2;

		private NumericUpDown nudHour;

		private NumericUpDown nudMintes;

		private NumericUpDown nudSecond;

		private ComboBox cbMoth;

		private ComboBox cbDay;

		private Label label3;

		private Label label4;

		private Label label5;

		private Label label6;

		private Label label7;

		private CheckBox ckBx;

		private Timer tmrTime;

		private Button btClose;

		private Button btRestart;

		private Button btCancellation;

		private Button btEsc;

		private TextBox txtRemind;

		private Label label8;

		private Button button1;

		private Label label9;

		private int gotime = 0;

		private string cmd = null;

		private int counttime = 0;

		private bool flag = true;

		private bool flag1 = true;

		private bool flag2 = true;

		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}

		private void InitializeComponent()
		{
			this.components = new Container();
			ComponentResourceManager resources = new ComponentResourceManager(typeof(定时关机));
			this.label1 = new Label();
			this.label2 = new Label();
			this.nudHour = new NumericUpDown();
			this.nudMintes = new NumericUpDown();
			this.nudSecond = new NumericUpDown();
			this.cbMoth = new ComboBox();
			this.cbDay = new ComboBox();
			this.label3 = new Label();
			this.label4 = new Label();
			this.label5 = new Label();
			this.label6 = new Label();
			this.label7 = new Label();
			this.ckBx = new CheckBox();
			this.tmrTime = new Timer(this.components);
			this.btClose = new Button();
			this.btRestart = new Button();
			this.btCancellation = new Button();
			this.btEsc = new Button();
			this.txtRemind = new TextBox();
			this.label8 = new Label();
			this.button1 = new Button();
			this.label9 = new Label();
			((ISupportInitialize)this.nudHour).BeginInit();
			((ISupportInitialize)this.nudMintes).BeginInit();
			((ISupportInitialize)this.nudSecond).BeginInit();
			base.SuspendLayout();
			this.label1.Anchor = AnchorStyles.None;
			this.label1.AutoSize = true;
			this.label1.Font = new Font("Microsoft YaHei UI", 13f);
			this.label1.Location = new Point(142, 57);
			this.label1.Name = "label1";
			this.label1.Size = new Size(82, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "系统时间";
			this.label2.Anchor = AnchorStyles.None;
			this.label2.AutoSize = true;
			this.label2.Font = new Font("Microsoft YaHei UI", 13f);
			this.label2.Location = new Point(52, 57);
			this.label2.Name = "label2";
			this.label2.Size = new Size(100, 23);
			this.label2.TabIndex = 1;
			this.label2.Text = "系统时间:";
			this.nudHour.Anchor = AnchorStyles.None;
			this.nudHour.Location = new Point(56, 127);
			this.nudHour.Name = "nudHour";
			this.nudHour.Size = new Size(78, 23);
			this.nudHour.TabIndex = 2;
			this.nudMintes.Anchor = AnchorStyles.None;
			this.nudMintes.Location = new Point(166, 127);
			this.nudMintes.Name = "nudMintes";
			this.nudMintes.Size = new Size(78, 23);
			this.nudMintes.TabIndex = 3;
			this.nudSecond.Anchor = AnchorStyles.None;
			this.nudSecond.Location = new Point(275, 127);
			this.nudSecond.Name = "nudSecond";
			this.nudSecond.Size = new Size(57, 23);
			this.nudSecond.TabIndex = 4;
			this.cbMoth.Anchor = AnchorStyles.None;
			this.cbMoth.FormattingEnabled = true;
			this.cbMoth.Location = new Point(166, 172);
			this.cbMoth.Name = "cbMoth";
			this.cbMoth.Size = new Size(78, 23);
			this.cbMoth.TabIndex = 5;
			this.cbMoth.SelectedIndexChanged  = this.cbMoth_SelectedIndexChanged;
			this.cbDay.Anchor = AnchorStyles.None;
			this.cbDay.FormattingEnabled = true;
			this.cbDay.Location = new Point(275, 172);
			this.cbDay.Name = "cbDay";
			this.cbDay.Size = new Size(57, 23);
			this.cbDay.TabIndex = 6;
			this.label3.AutoSize = true;
			this.label3.Font = new Font("Microsoft YaHei UI", 11f);
			this.label3.Location = new Point(246, 131);
			this.label3.Name = "label3";
			this.label3.RightToLeft = RightToLeft.No;
			this.label3.Size = new Size(24, 19);
			this.label3.TabIndex = 7;
			this.label3.Text = "分";
			this.label4.AutoSize = true;
			this.label4.Font = new Font("Microsoft YaHei UI", 11f);
			this.label4.Location = new Point(137, 131);
			this.label4.Name = "label4";
			this.label4.RightToLeft = RightToLeft.No;
			this.label4.Size = new Size(24, 19);
			this.label4.TabIndex = 8;
			this.label4.Text = "时";
			this.label5.AutoSize = true;
			this.label5.Font = new Font("Microsoft YaHei UI", 11f);
			this.label5.Location = new Point(339, 131);
			this.label5.Name = "label5";
			this.label5.RightToLeft = RightToLeft.No;
			this.label5.Size = new Size(24, 19);
			this.label5.TabIndex = 9;
			this.label5.Text = "秒";
			this.label6.AutoSize = true;
			this.label6.Font = new Font("Microsoft YaHei UI", 11f);
			this.label6.Location = new Point(246, 176);
			this.label6.Name = "label6";
			this.label6.RightToLeft = RightToLeft.No;
			this.label6.Size = new Size(24, 19);
			this.label6.TabIndex = 10;
			this.label6.Text = "月";
			this.label7.AutoSize = true;
			this.label7.Font = new Font("Microsoft YaHei UI", 11f);
			this.label7.Location = new Point(339, 176);
			this.label7.Name = "label7";
			this.label7.RightToLeft = RightToLeft.No;
			this.label7.Size = new Size(24, 19);
			this.label7.TabIndex = 11;
			this.label7.Text = "日";
			this.ckBx.AutoSize = true;
			this.ckBx.Location = new Point(61, 176);
			this.ckBx.Name = "ckBx";
			this.ckBx.Size = new Size(74, 19);
			this.ckBx.TabIndex = 12;
			this.ckBx.Text = "开启定时";
			this.ckBx.UseVisualStyleBackColor = true;
			this.ckBx.CheckedChanged  = this.ckBx_CheckedChanged;
			this.tmrTime.Tick  = this.tmrTime_Tick;
			this.btClose.Enabled = false;
			this.btClose.Location = new Point(56, 241);
			this.btClose.Name = "btClose";
			this.btClose.Size = new Size(75, 33);
			this.btClose.TabIndex = 13;
			this.btClose.Text = "关机";
			this.btClose.UseVisualStyleBackColor = true;
			this.btClose.Click  = this.btClose_Click;
			this.btRestart.Enabled = false;
			this.btRestart.Location = new Point(166, 241);
			this.btRestart.Name = "btRestart";
			this.btRestart.Size = new Size(75, 33);
			this.btRestart.TabIndex = 14;
			this.btRestart.Text = "重启";
			this.btRestart.UseVisualStyleBackColor = true;
			this.btRestart.Click  = this.btRestart_Click;
			this.btCancellation.Enabled = false;
			this.btCancellation.Location = new Point(275, 241);
			this.btCancellation.Name = "btCancellation";
			this.btCancellation.Size = new Size(75, 33);
			this.btCancellation.TabIndex = 15;
			this.btCancellation.Text = "注销";
			this.btCancellation.UseVisualStyleBackColor = true;
			this.btCancellation.Click  = this.btCancellation_Click;
			this.btEsc.Location = new Point(348, 3);
			this.btEsc.Name = "btEsc";
			this.btEsc.Size = new Size(56, 38);
			this.btEsc.TabIndex = 16;
			this.btEsc.Text = "退出";
			this.btEsc.UseVisualStyleBackColor = true;
			this.btEsc.Click  = this.btEsc_Click;
			this.txtRemind.Font = new Font("Microsoft YaHei UI", 13f);
			this.txtRemind.Location = new Point(121, 330);
			this.txtRemind.Multiline = true;
			this.txtRemind.Name = "txtRemind";
			this.txtRemind.Size = new Size(179, 29);
			this.txtRemind.TabIndex = 17;
			this.label8.AutoSize = true;
			this.label8.Font = new Font("Microsoft YaHei UI", 14f);
			this.label8.Location = new Point(29, 330);
			this.label8.Name = "label8";
			this.label8.Size = new Size(86, 24);
			this.label8.TabIndex = 18;
			this.label8.Text = "倒计时:";
			this.button1.Location = new Point(335, 330);
			this.button1.Name = "button1";
			this.button1.Size = new Size(50, 29);
			this.button1.TabIndex = 19;
			this.button1.Text = "取消";
			this.button1.UseVisualStyleBackColor = true;
			this.button1.Click  = this.button1_Click_1;
			this.label9.Anchor = AnchorStyles.None;
			this.label9.AutoSize = true;
			this.label9.Font = new Font("Microsoft YaHei UI", 9f);
			this.label9.Location = new Point(53, 109);
			this.label9.Name = "label9";
			this.label9.Size = new Size(58, 15);
			this.label9.TabIndex = 20;
			this.label9.Text = "设置时间:";
			this.label9.Click  = this.label9_Click;
			base.AutoScaleDimensions = new SizeF(7f, 15f);
			base.AutoScaleMode = AutoScaleMode.Font;
			base.ClientSize = new Size(416, 430);
			base.ControlBox = false;
			base.Controls.Add(this.label9);
			base.Controls.Add(this.button1);
			base.Controls.Add(this.label8);
			base.Controls.Add(this.txtRemind);
			base.Controls.Add(this.btEsc);
			base.Controls.Add(this.btCancellation);
			base.Controls.Add(this.btRestart);
			base.Controls.Add(this.btClose);
			base.Controls.Add(this.ckBx);
			base.Controls.Add(this.label7);
			base.Controls.Add(this.label6);
			base.Controls.Add(this.label5);
			base.Controls.Add(this.label4);
			base.Controls.Add(this.label1);
			base.Controls.Add(this.label2);
			base.Controls.Add(this.nudHour);
			base.Controls.Add(this.nudMintes);
			base.Controls.Add(this.cbDay);
			base.Controls.Add(this.label3);
			base.Controls.Add(this.cbMoth);
			base.Controls.Add(this.nudSecond);
			this.Font = new Font("Microsoft YaHei UI", 9f);
			base.FormBorderStyle = FormBorderStyle.FixedSingle;
			//base.Icon = (Icon)resources.GetObject("$this.Icon");
			base.Margin = new Padding(3, 4, 3, 4);
			base.MaximizeBox = false;
			base.MinimizeBox = false;
			base.Name = "定时关机";
			base.StartPosition = FormStartPosition.CenterScreen;
			this.Text = "定时关机";
			base.Load  = this.Form1_Load;
			((ISupportInitialize)this.nudHour).EndInit();
			((ISupportInitialize)this.nudMintes).EndInit();
			((ISupportInitialize)this.nudSecond).EndInit();
			base.ResumeLayout(false);
			base.PerformLayout();
		}

		public 定时关机()
		{
			this.InitializeComponent();
		}

		public void allow()
		{
			DialogResult Result = MessageBox.Show("程序将自动化到托盘", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
			if (Result == DialogResult.Yes)
			{
				this.windowHide();
			}
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			Label label = this.label1;
			DateTime now = DateTime.Now;
			label.Text = now.ToString("yyy年MM月dd天HH:mm:ss");
			this.label1.ForeColor = Color.Red;
			this.nudHour.Maximum = 23m;
			this.nudMintes.Maximum = 59m;
			this.nudSecond.Maximum = 59m;
			ComboBox comboBox = this.cbMoth;
			now = DateTime.Now;
			int num = now.Month;
			comboBox.Text = num.ToString();
			ComboBox comboBox2 = this.cbDay;
			now = DateTime.Now;
			num = now.Day;
			comboBox2.Text = num.ToString();
			NumericUpDown numericUpDown = this.nudHour;
			now = DateTime.Now;
			numericUpDown.Value = now.Hour;
			NumericUpDown numericUpDown2 = this.nudMintes;
			now = DateTime.Now;
			numericUpDown2.Value = now.Minute;
			NumericUpDown numericUpDown3 = this.nudSecond;
			now = DateTime.Now;
			numericUpDown3.Value = now.Second;
			this.tmrTime.Interval = 1000;
			this.tmrTime.Enabled = true;
			for (int j = 1; j < 13; j  )
			{
				this.cbMoth.Items.Add(j);
			}
			for (int i = 1; i < 32; i  )
			{
				this.cbDay.Items.Add(i);
			}
			this.cbMoth.Enabled = false;
			this.cbDay.Enabled = false;
			this.nudHour.Enabled = false;
			this.nudMintes.Enabled = false;
			this.nudSecond.Enabled = false;
		}

		private void numericUpDown2_ValueChanged(object sender, EventArgs e)
		{
		}

		private void checkBox1_CheckedChanged(object sender, EventArgs e)
		{
		}

		private void ckBx_CheckedChanged(object sender, EventArgs e)
		{
			if (this.ckBx.Checked)
			{
				this.cbMoth.Enabled = true;
				this.cbDay.Enabled = true;
				this.nudHour.Enabled = true;
				this.nudMintes.Enabled = true;
				this.nudSecond.Enabled = true;
				this.btClose.Enabled = true;
				this.btCancellation.Enabled = true;
				this.btRestart.Enabled = true;
				this.btEsc.Enabled = false;
			}
			else
			{
				this.cbMoth.Enabled = false;
				this.cbDay.Enabled = false;
				this.nudHour.Enabled = false;
				this.nudMintes.Enabled = false;
				this.nudSecond.Enabled = false;
				this.btClose.Enabled = false;
				this.btCancellation.Enabled = false;
				this.btRestart.Enabled = false;
				this.btEsc.Enabled = true;
			}
		}

		private void windowShow()
		{
			base.Show();
			base.ShowInTaskbar = true;
			base.WindowState = FormWindowState.Normal;
		}

		private void windowHide()
		{
			base.Hide();
			base.ShowInTaskbar = false;
		}

		private void cbMoth_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				int temp2 = int.Parse(this.cbMoth.Text);
				int temp = int.Parse(this.cbDay.Text);
				DateTime now;
				if (temp2 < 1 || temp2 > 12)
				{
					ComboBox comboBox = this.cbMoth;
					now = DateTime.Now;
					comboBox.Text = now.Month.ToString();
				}
				if (this.cbMoth.Text == "2")
				{
					this.cbDay.Items.Clear();
					now = DateTime.Now;
					int year = now.Year;
					if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
					{
						for (int l = 1; l <= 29; l  )
						{
							this.cbDay.Items.Add(l);
						}
					}
					else
					{
						for (int l = 1; l <= 28; l  )
						{
							this.cbDay.Items.Add(l);
						}
					}
				}
				else if (this.cbMoth.Text == "4" || this.cbMoth.Text == "6" || this.cbMoth.Text == "9" || this.cbMoth.Text == "11")
				{
					this.cbDay.Items.Clear();
					for (int l = 1; l <= 30; l  )
					{
						this.cbDay.Items.Add(l);
					}
				}
				else
				{
					this.cbDay.Items.Clear();
					for (int l = 1; l <= 31; l  )
					{
						this.cbDay.Items.Add(l);
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show("错误:"   ex);
			}
		}

		private bool timecontrol()
		{
			if (this.ckBx.Checked)
			{
				string[] array = new string[11]
				{
					DateTime.Now.Year.ToString(),
					"/",
					this.cbMoth.Text,
					"/",
					this.cbDay.Text,
					" ",
					null,
					null,
					null,
					null,
					null
				};
				string[] array2 = array;
				decimal value = this.nudHour.Value;
				array2[6] = value.ToString();
				array[7] = ":";
				string[] array3 = array;
				value = this.nudMintes.Value;
				array3[8] = value.ToString();
				array[9] = ":";
				string[] array4 = array;
				value = this.nudSecond.Value;
				array4[10] = value.ToString();
				DateTime startTime = Convert.ToDateTime(string.Concat(array));
				TimeSpan endTime = startTime - DateTime.Now;
				this.counttime = endTime.Days * 864800   endTime.Hours * 3600   endTime.Minutes * 60   endTime.Seconds;
			}
			if (this.counttime < 0)
			{
				MessageBox.Show("时间输入有误,请重新输入!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.cbMoth.Enabled = true;
				this.cbDay.Enabled = true;
				this.nudHour.Enabled = true;
				this.nudMintes.Enabled = true;
				this.nudSecond.Enabled = true;
				this.btClose.Enabled = true;
				this.btCancellation.Enabled = true;
				this.btRestart.Enabled = true;
				this.btEsc.Enabled = false;
				this.tmrTime.Enabled = false;
				return false;
			}
			this.tmrTime.Enabled = true;
			return true;
		}

		private void btEsc_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void tmrTime_Tick(object sender, EventArgs e)
		{
			this.label1.Text = DateTime.Now.ToString("yyy年MM月dd日HH:mm:ss");
			if (this.cmd != null)
			{
				this.gotime  ;
				if (this.counttime - this.gotime   5 < 0)
				{
					this.txtRemind.Text = "0";
					this.cmd = null;
				}
				else
				{
					int temp = this.counttime - this.gotime   1;
					this.txtRemind.Text = temp / 864800   "天"   temp % 864800 / 3600   "时"   temp % 3600 / 60   "分"   temp % 60   "秒";
					if (this.counttime - this.gotime   1 == 0)
					{
						ProcessStartInfo P = new ProcessStartInfo("shutdown.exe", this.cmd);
						P.WindowStyle = ProcessWindowStyle.Hidden;
						Process.Start(P);
						Application.ExitThread();
						Application.Exit();
					}
				}
			}
		}

		private void btClose_Click(object sender, EventArgs e)
		{
			this.flag = false;
			if (this.flag1 && this.flag2)
			{
				this.btClose.ForeColor = Color.Red;
				if (this.timecontrol())
				{
					this.cmd = "-s -t 0";
					this.allow();
				}
			}
		}

		private void btRestart_Click(object sender, EventArgs e)
		{
			this.flag1 = false;
			if (this.flag && this.flag2)
			{
				this.btRestart.ForeColor = Color.Red;
				if (this.timecontrol())
				{
					this.cmd = "-r -t 0";
					this.allow();
				}
			}
		}

		private void btCancellation_Click(object sender, EventArgs e)
		{
			this.flag2 = false;
			if (this.flag && this.flag1)
			{
				this.btCancellation.ForeColor = Color.Red;
				if (this.timecontrol())
				{
					this.cmd = "-l";
					this.allow();
				}
			}
		}

		private void button1_Click_1(object sender, EventArgs e)
		{
			this.cmd = null;
			this.gotime = 0;
			this.counttime = 0;
			this.txtRemind.Text = "";
			this.btClose.ForeColor = Color.Black;
			this.btRestart.ForeColor = Color.Black;
			this.btCancellation.ForeColor = Color.Black;
			this.flag = true;
			this.flag1 = true;
			this.flag2 = true;
			MessageBox.Show("任务已被成功取消", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
		}

		private void label9_Click(object sender, EventArgs e)
		{
		}
	}
}


标签: 定时器 定时

实例下载地址

C# 定时器例子(定时关机/重启/注销)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警