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

C#定时关机、重启、注销

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.13M
  • 下载次数:76
  • 浏览次数:445
  • 发布时间:2020-11-04
  • 实例类别:C#语言基础
  • 发 布 人:ZINGU
  • 文件格式:.rar
  • 所需积分:1
 相关标签: 定时关机 关机 定时 C#

实例介绍

【实例简介】

C#定时关机、重启、注销

支持最小化推盘。

30S倒计时提醒。

【实例截图】

from clipboard

from clipboard


【核心代码】

public partial class Form1 : Form
    { 
        private bool STATE = false;
        private long sec;
        private long ms;
        private string  type="";
        System.Timers.Timer t1 = new System.Timers.Timer(1000); 
        System.Timers.Timer t2 =  new System.Timers.Timer(1000);
        System.Timers.Timer t3 = new System.Timers.Timer(1);
        //System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
        public Form1()
        {
            InitializeComponent(); 
            t1.Elapsed = new System.Timers.ElapsedEventHandler(timeout1);
            t2.Elapsed = new System.Timers.ElapsedEventHandler(timeout2);
            t3.Elapsed = new System.Timers.ElapsedEventHandler(timeout3); 
           // this.tabControl1.SelectedIndexChanged = new EventHandler(tabControl1_SelectedIndexChanged);
           // t.Interval = 1;
           // t.Enabled = true;
           // this.t.Tick = new System.EventHandler(this.t_Tick);
            LoadCommbox();
            Init();
        }
        private void t_Tick(object sender, System.EventArgs e)
        {
            if (STATE)
            {
                ShowCountDown();
                ShowCountDownER();
            }
        }
        public void timeout1(object source, System.Timers.ElapsedEventArgs e)
        { 
            ShowNowTime();  
        }
        public void timeout2(object source, System.Timers.ElapsedEventArgs e)
        {
            if (STATE)
            {
                ShowCountDown();
            } 
        }
        public void timeout3(object source, System.Timers.ElapsedEventArgs e)
        {
            if (STATE)
            {
                ShowCountDownER();
            } 
        }
        /// <summary>
        /// 当前时间
        /// </summary>
        private void ShowNowTime()
        { 
            Thread.Sleep(1000);
            lbNowTime.Text = "北京时间:" DateTime.Now.ToLongDateString().ToString() DateTime.Now.ToLongTimeString().ToString();  
        }
        /// <summary>
        /// 倒计时
        /// </summary>
        private void ShowCountDown()
        {
            //计算多少秒 
            int days = Convert.ToInt32(sec / (24 * 3600));
            int hour = Convert.ToInt32((sec - days * 24 * 3600) / 3600);
            int mint = Convert.ToInt32((sec - days * 24 * 3600 - hour * 3600) / 60);
            int secs = Convert.ToInt32((sec - days * 24 * 3600 - hour * 3600 - mint*60));
            lbCountdown.Text = "倒计时:" days "天" hour "时" mint "分" secs "秒";
            sec--; 

        }
 

        private long GetSecond()
        {
            long lo;
            if (radioButton1.Checked)//倒计时
            {
                lo = long.Parse((numericUpDownHour.Value * 3600 numericUpDownMin.Value * 60 numericUpDownSec.Value).ToString());
            }
            else//固定时间
            {
                DateTime dt1 = DateTime.Now;
                DateTime dt2 = GetSetDateTime(); 
                TimeSpan ts = dt1.Subtract(dt2).Duration();
                lo = ts.Days * 24 * 3600 ts.Hours * 3600 ts.Minutes * 60 ts.Seconds; 
            }
            return lo;
        }

        private DateTime GetSetDateTime()
        {
            return DateTime.Parse(dateTimePicker1.Text " " comboBox2.Text ":" comboBox3.Text ":" comboBox4.Text);
        }

        /// <summary>
        /// 到计时器
        /// </summary>
        private void ShowCountDownER()
        { 
            lbCountdownER.Text = "倒计时器:" ms "ms";
            TimeOut();
            ms--; 
        }

        /// <summary>
        /// 到点执行
        /// </summary>
        private void TimeOut()
        {
            if (ms == 0)
            {
                STATE = false;
                t2.Stop();
                t3.Stop();
                //t.Stop();
                WaitMinute wm = new WaitMinute(type); 
                if (wm.ShowDialog() == DialogResult.OK)
                {
                    Doit();
                }
                return;
            } 
        }

        /// <summary>
        /// 加载下拉
        /// </summary>
        private void LoadCommbox()
        {
            //时
            for (int i = 0; i < 24; i )
            {
                comboBox2.Items.Add(i.ToString().PadLeft(2, '0'));
            }
            //分秒
            for (int i = 0; i < 60; i )
            {
                comboBox3.Items.Add(i.ToString().PadLeft(2,'0'));
                comboBox4.Items.Add(i.ToString().PadLeft(2,'0'));
            } 
        }

        /// <summary>
        /// 初始化窗体
        /// </summary>
        private void Init()
        {
            //this.MinimizeBox = false;
            this.MaximizeBox = false;
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0; 
            comboBox3.SelectedIndex = 0;
            comboBox4.SelectedIndex = 0;
            radioButton1.Select();
            t1.Start();  
        }

        /// <summary>
        /// 执行操作
        /// </summary>
        private void Doit()
        {
            switch (comboBox1.SelectedItem.ToString())
            {
                case "关机":
                    Process.Start("shutdown.exe", "-s");
                    break;
                case "重启":
                    Process.Start("shutdown.exe", "-r -t 10");
                    break;
                case "注销":
                    Process.Start("shutdown.exe", "-l");
                    break;
                default:
                    break;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //检查
            if (button1.Text == "开始")
            {
                button1.Text = "停止";
                t2.Start();
                t3.Start();
            }
            else
            {
                button1.Text = "开始";
                t2.Stop();
                t3.Stop(); 
            }
            CheckSetting();
            type = comboBox1.SelectedItem.ToString();
            STATE = true;
            sec = GetSecond();
            ms = sec * 100; 
            
           
             
        }
        /// <summary>
        /// 检查选中时间不能小于当前时间
        /// </summary>
        private void CheckSetting()
        {
            try
            { 
                if (radioButton2.Checked && GetSetDateTime() < DateTime.Now)
                {
                    MessageBox.Show("设定时间小于当前时间,关个锤子?");
                    return;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
        /// <summary>
        /// 折叠-展开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.Text == "折叠")
            {
                button2.Text = "展开";
                
                for (int i = 0; i < 14; i )
                {
                    this.Height= this.Height - i;
                    Thread.Sleep(100);
                }
            }
            else
            {
                button2.Text = "折叠";
                for (int i = 0; i < 14; i )
                {
                    this.Height = this.Height i;
                    Thread.Sleep(100);
                }
            }
        }

        /*
        private void timer1_Tick(object sender, EventArgs e)
        {
            ShowNowTime();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            ShowCountDown();
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            ShowCountDownER();
        }
        */ 

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
             this.Visible=   true; 
             this.WindowState = FormWindowState.Normal;  
        }

        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ShowInTaskbar = true; 
            WindowState = FormWindowState.Normal;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dr; 
            dr = MessageBox.Show("确认退出吗", "确认对话框", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (dr == DialogResult.Yes) 
            {
                // 关闭所有的线程
                this.Dispose();
                this.Close();
            } 
            else
            { 
                e.Cancel = true; 
            } 
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定退出?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK)
            {
                // 关闭所有的线程
                this.Dispose();
                this.Close();
            }
        } 

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.ShowInTaskbar = false;
            } 
        }
  
    }
}


实例下载地址

C#定时关机、重启、注销

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

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

网友评论

发表评论

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

查看所有0条评论>>

小贴士

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

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

关于好例子网

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

;
报警