电脑每次点击休眠后,不断电的情况下,电脑总是莫名其妙的自己开机,写一个程序搞定

电脑每次点击休眠后,不断电的情况下,电脑总是莫名其妙的自己开机,写一个程序搞定

 

程序写的很简陋,满足了我的要求就好啦????????????

源代码地址:https://download.csdn.net/download/huyangvista/12507983

源代码地址 百度云: 链接:https://pan.baidu.com/s/1MRo654GP_GNMEjWY7LTDvw 
提取码:obv1 
复制这段内容后打开百度网盘手机App,操作更方便哦

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsAutoShutdown
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("PowrProf.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        //休眠
        public static extern bool SetSuspendState(bool hiberate, bool forceCritical, bool disableWakeEvent);
        [DllImport("user32")]
        public static extern bool ExitWindowsEx(uint uFlags, uint dwReason);
        [DllImport("user32")]
        public static extern void LockWorkStation();


        public static bool isDebug = false;
        public enum EShutdownType
        {
            Shutdown, ShutdownReset, ShutdownLogout, ShutdownLock, ShutdownSleep, ShutdownRest
        }
        private EShutdownType shutdownType = EShutdownType.ShutdownRest;

        //关机
        public static void Shutdown() 
        {
            if (isDebug) {
                MessageBox.Show("执行了关机");
                return;
            } 
            Process.Start("shutdown", "/s /t 0");    // 参数 /s 的意思是要关闭计算机
        }

        //重启
        public static void ShutdownReset()
        {
            if (isDebug)
            {
                MessageBox.Show("执行了重启");
                return;
            }
            Process.Start("shutdown", "/r /t 0"); // 参数 /r 的意思是要重新启动计算机
        }

        //注销
        public static void ShutdownLogout()
        {
            if (isDebug)
            {
                MessageBox.Show("执行了注销");
                return;
            }
            ExitWindowsEx(0, 0);
        }
        //锁定
        public static void ShutdownLock()
        {
            if (isDebug)
            {
                MessageBox.Show("执行了锁定");
                return;
            }
            LockWorkStation();
        }
        //睡眠
        public static void ShutdownSleep()
        {
            if (isDebug)
            {
                MessageBox.Show("执行了睡眠");
                return;
            }
            SetSuspendState(false, true, true);
        }
        //休眠
        public static void ShutdownRest()
        {
            if (isDebug)
            {
                MessageBox.Show("执行了休眠");
                return;
            }
            SetSuspendState(true, true, true);
        }

        private string FormatTime(object text)
        {
            return text.ToString().PadLeft(2, '0');
        }

        private void Form1_Load(object sender, EventArgs e)
        {


        }

        private Thread threadStart = null;
        private Thread threadAction = null;

        private void button1_Click(object sender, EventArgs e)
        {
            if (threadStart != null) {
                MessageBox.Show("当前存在任务,请结束后再执行");
                return;
            }
            var actionTime = Convert.ToSingle(textBox1.Text) * 3600 + Convert.ToSingle(textBox2.Text) * 60;
            var time = 0;
            var thread = new Thread(() => {
                while (true)
                {
                    this.BeginInvoke(new Action(() => {
                        var timeDesc = (int)(actionTime - time);
                        button1.Text = "定时开启 倒计时: " + timeDesc + "s";
                        label8.Text = FormatTime(timeDesc / 3600) + ":" + FormatTime(timeDesc % 3600 / 60) + ":" + FormatTime(timeDesc % 60);
                    }));

                    if (time >= actionTime)
                    {
                        File.AppendAllText("./log.txt", "启动休眠\r\n");
                        time = 0;

                        button2_Click(null, null);
                        break;
                    }
                    Thread.Sleep(1000);
                    time++;
                }
            });
            thread.IsBackground = true;
            thread.Start();
            threadStart = thread;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (threadAction != null)
            {
                MessageBox.Show("当前存在任务,请结束后再执行");
                return;
            }
            var actionTime = Convert.ToSingle(textBox4.Text) * 3600 + Convert.ToSingle(textBox3.Text) * 60;
            var time = 0;
            var thread = new Thread(() => {
                while (true)
                {
                    this.BeginInvoke(new Action(() => {
                        var timeDesc = (int)(actionTime - time);
                        button2.Text = "循环执行 倒计时: " + timeDesc + "s";
                        label9.Text = FormatTime(timeDesc / 3600) + ":" + FormatTime(timeDesc % 3600 / 60) + ":" + FormatTime(timeDesc % 60);
                    }));

                    if (time >= actionTime)
                    {
                        File.AppendAllText("./log.txt", "自动休眠\r\n");
                        time = 0;

                        //SetSuspendState(true, true, true);
                        ShutdownAction();
                    }
                    Thread.Sleep(1000);
                    time++;
                }
            });
            thread.IsBackground = true;
            thread.Start();
            threadAction = thread;

        }

        private void ShutdownAction() {

            switch (shutdownType)
            {
                case EShutdownType.Shutdown:
                    Shutdown();
                    break;
                case EShutdownType.ShutdownReset:
                    ShutdownReset();
                    break;
                case EShutdownType.ShutdownLogout:
                    ShutdownLogout();
                    break;
                case EShutdownType.ShutdownLock:
                    ShutdownLock();
                    break;
                case EShutdownType.ShutdownSleep:
                    ShutdownSleep();
                    break;
                case EShutdownType.ShutdownRest:
                    ShutdownRest();
                    break;
                default:
                    break;
            }

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var text = comboBox1.Text;
            switch (text)
            {
                case "关机":
                    shutdownType = EShutdownType.Shutdown;
                    break;
                case "重启":
                    shutdownType = EShutdownType.ShutdownReset;
                    break;
                case "注销":
                    shutdownType = EShutdownType.ShutdownLogout;
                    break;
                case "锁定":
                    shutdownType = EShutdownType.ShutdownLock;
                    break;
                case "睡眠":
                    shutdownType = EShutdownType.ShutdownSleep;
                    break;
                case "休眠":
                    shutdownType = EShutdownType.ShutdownRest;
                    break;
                default:
                    break;
            }
        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (threadStart != null)
            {
                threadStart.Abort();
                threadStart = null;
                button1.Text = "定时开启";
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (threadAction != null) {
                threadAction.Abort();
                threadAction = null;
                button2.Text = "循环执行";
            }
        }
    }
}