簡體   English   中英

ShutDown C#項目

[英]ShutDown C# Project

我目前正在編寫一個程序,當按下一個按鈕時應該關閉/重新啟動,但是當我按下關閉或重新啟動按鈕時,該操作將立即完成,而無需等待(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 ....");
            }


        }
    }
}

使用shutdown-t開關指定等待時間

shutdown {-r|-s} -t xxx

其中xxx是等待的秒數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM