简体   繁体   English

C#:如何唤醒已关机的系统?

[英]C#: How to wake up system which has been shutdown?

Is there any Win32 API for waking up a system that has been shut down, at a specific time? 是否有任何Win32 API用于唤醒在特定时间关闭的系统? I have seen a program named Autopower On which is able to power the system on at a specified time. 我见过一个名为Autopower On的程序,可以在指定的时间启动系统。

Got the below post from a site. 从网站获得以下帖子 Any body tried this? 有人试过这个吗?

Nothing in the framework, so you'll have to "PInvoke" a bit. 框架中没有任何东西,所以你必须“PInvoke”一点。 The API's you need to call are CreateWaitableTimer and SetWaitableTimer. 您需要调用的API是CreateWaitableTimer和SetWaitableTimer。 Following is a complete (Q&D) sample that illustrates how you can set a system to be awoken from sleep/hibernate using above Win32 API's. 以下是一个完整的(Q&D)示例,演示了如何使用上面的Win32 API将系统设置为从睡眠/休眠状态唤醒。 Note that here I'm setting a relative wakeup time of 300000000 nSecs. 请注意,这里我设置的相对唤醒时间为3000000 nSecs。 That means that the computer will wake-up (supposing he's asleep or hibernating) within 30 seconds after setting the timer. 这意味着计算机将在设置计时器后30秒内唤醒(假设他正在睡觉或休眠)。 Consult the MSDN docs for details on SetWaitableTimer and it's arguments. 有关SetWaitableTimer及其参数的详细信息,请参阅MSDN文档。

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Willys
{
    class Program
    {
        [DllImport("kernel32.dll")]
        public static extern IntPtr CreateWaitableTimer(IntPtr lpTimerAttributes,
        bool bManualReset, string lpTimerName);

        [DllImport("kernel32.dll")]
        public static extern bool SetWaitableTimer(IntPtr hTimer, [In] ref long
        pDueTime, int lPeriod, IntPtr pfnCompletionRoutine, IntPtr
        lpArgToCompletionRoutine, bool fResume);

        [DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
        public static extern Int32 WaitForSingleObject(IntPtr handle, uint
        milliseconds);

        static void Main()
        {
            SetWaitForWakeUpTime();
        }

        static IntPtr handle;
        static void SetWaitForWakeUpTime()
        {
            long duetime = -300000000; // negative value, so a RELATIVE due time
            Console.WriteLine("{0:x}", duetime);
            handle = CreateWaitableTimer(IntPtr.Zero, true, "MyWaitabletimer");
            SetWaitableTimer(handle, ref duetime, 0, IntPtr.Zero, IntPtr.Zero, true);
            uint INFINITE = 0xFFFFFFFF;
            int ret = WaitForSingleObject(handle, INFINITE);
            MessageBox.Show("Wake up call");
        }
    }
}

Wake on LAN(WOL)代码项目帖子可能有用(主板和NIC都必须支持WOL)

Once you actually shut down your computer(not sleeping or hibernating) you can't wake it up from C#, C++ code directly. 一旦你真正关闭了你的计算机(不睡觉或休眠),你就无法直接从C#,C ++代码中唤醒它。 After all the OS itself is closed. 在所有操作系统本身关闭之后。

The only chance would be for your motherboard to support some kind of timer mechanism. 唯一的机会是你的主板支持某种计时器机制。 And with some C++ function to be able to write some flags into the BIOS and set that timer. 并且有一些C ++函数能够将一些标志写入BIOS并设置该计时器。

Use the windows "Scheduled Task" menu. 使用Windows“计划任务”菜单。 See: 看到:

http://www.howtogeek.com/119028/how-to-make-your-pc-wake-from-sleep-automatically/ http://www.howtogeek.com/119028/how-to-make-your-pc-wake-from-sleep-automatically/

You create a program that you want to run upon wake up and then manually enter it into the windows scheduled task list for the first time to wake after a specific time. 您创建一个要在唤醒时运行的程序,然后在特定时间后第一次将其手动输入到Windows计划任务列表中。

When you program awakens, I am not sure if you need your program to call a Windows API to stop it being awoken again, ditto if you want your program to power off the PC after starting another sceduled task to awaken it again. 当你编程唤醒时,我不确定你是否需要你的程序来调用Windows API以阻止它再次被唤醒,同样如果你希望你的程序在启动另一个已调度的任务再次唤醒之后关闭PC。 Need to reserach what windows APIs there are for this. 需要研究一下这些Windows API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM