0

我目前正在编写一个在按下按钮时应该关闭/重新启动的程序,但是当我按下关闭或重新启动按钮时,操作会立即完成,而无需等待(datetimepicker)中的指定时间。

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication8
{
    public partial class Form2 : Form
    {
        private DateTime tm;
        public Form2()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (DateTime.Now.CompareTo(tm) > 0)
            {
                Sti();
                System.Diagnostics.Process.Start("shutdown", "/s");
            }
            else
            {
                MessageBox.Show("your computer will shutdown shortly ....");
            }

        }
        private void Sti()
        {
            if (DateTime.Now.TimeOfDay.CompareTo(tm.TimeOfDay) > 0)
            {
                tm = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day + 1, tm.Hour, tm.Minute, tm.Second);
            }
            else
            {
                tm = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, tm.Hour, tm.Minute, tm.Second);
            }

        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (DateTime.Now.CompareTo(tm) >= 0)
            {
                Sti();
                System.Diagnostics.Process.Start("shutdown", "/r");
            }
            else
            {
                MessageBox.Show("your computer will shutdown shortly ....");
            }


        }
    }
}
4

1 回答 1

1

使用-t开关shutdown指定等待时间

shutdown {-r|-s} -t xxx

其中 xxx 是等待的秒数。

于 2013-02-09T19:57:07.837 回答