command to wake up the windows system after sleep

2019-06-28 08:21发布

From C# program I want to call windows command prompt and make the system to sleep and after few seconds I should wake the system. I am successful in making the system sleep but I can't wake it up after few seconds. The command that I tried for making it sleep and wake up is "powrprof.dll,SetSuspendState 0,1,0 && timeout 10 && echo "Hello World" ".

The link http://www.groovypost.com/howto/schedule-wake-sleep-windows-automatically/ says echo will wake the system but its not working.

1条回答
【Aperson】
2楼-- · 2019-06-28 08:47

It is possible using the win32 CreateWaitableTimer API.

  • The process that creates the WaitableTimer has to be alive when the system sleep is called.
  • Example uses pywin32 api.
  • Allow timers to wake system from sleep in your Windows power configuration.

    from win32event import CreateWaitableTimer
    from win32event import SetWaitableTimer
    from win32event import WaitForSingleObject
    import sys

    def main(minutes):
        minutes = int(minutes)
        handle = CreateWaitableTimer(None, True, 'Wake up')
        dt = -10000000 * minutes * 60 # Convert to seconds.
        SetWaitableTimer(handle, dt, 0, None, None, True)
        rc = WaitForSingleObject(handle, 1000 * (minutes + 1) * 60) # 11 s.
        print ("Done")

    if __name__ == '__main__':
        minutes = sys.argv[1]
        main(minutes)

For running the above script call wakeupWindows.py from console like:

python wakeupWindows.py 2

Will set the wake up timer to 2 minutes from now. The script will wait for 2 + 1 = 3 minutes. From another console put the system off to sleep using:

shutdown /h

After 2 minutes, the system will wake up from sleep.

Similar implementations are possible using C# or this script can be converted to an exe using pyinstaller as explained here.

查看更多
登录 后发表回答