【发布时间】:2017-01-08 22:29:04
【问题描述】:
我想延迟关闭 Windows 10,以便我的应用可以完成一些必要的操作,例如保存数据...
以下是实现延迟的工作代码示例,但是 windows 在等待大约 1 分钟后取消关闭。
如何在不让 Windows 在一分钟后中止关机的情况下实现延迟? (我的应用可能需要 2 或 4 分钟才能完成)
Public Class Form1
Declare Function ShutdownBlockReasonCreate Lib "user32.dll" (ByVal hWnd As IntPtr, <Runtime.InteropServices.MarshalAs(Runtime.InteropServices.UnmanagedType.LPWStr)> ByVal reason As String) As Boolean
Declare Function ShutdownBlockReasonDestroy Lib "user32.dll" (ByVal hWnd As IntPtr) As Boolean
Protected Overrides Sub WndProc(ByRef aMessage As Message)
Const WM_QUERYENDSESSION As Integer = &H11
Const WM_ENDSESSION As Integer = &H16
If aMessage.Msg = WM_QUERYENDSESSION OrElse aMessage.Msg = WM_ENDSESSION Then
' Block shutdown
ShutdownBlockReasonCreate(Me.Handle, "Testing 123...")
' Do work
CleanUpAndSave()
' Continue with shutdown
ShutdownBlockReasonDestroy(Me.Handle)
Return
End If
MyBase.WndProc(aMessage)
End Sub
Private Sub CleanUpAndSave()
Dim sw As New Stopwatch
' Pretend work for 3 minutes
sw.Start()
Do While sw.ElapsedMilliseconds < 180000
Application.DoEvents()
Loop
sw.Stop()
End Sub
请提供工作代码(如果 Windows 完全可以的话)。
谢谢
【问题讨论】:
标签: vb.net windows-10