问题描述
我想延迟关闭 Windows 10,以便我的应用程序可以完成一些必要的操作,例如保存数据...
I want to delay windows 10 from shutting down so that my app can finish up some necessary actions like saving data...
以下是实现延迟的工作代码示例,但 Windows 在等待约 1 分钟后取消关闭.
Following is a working code example to achieve a delay, however windows cancels the shutdown after about 1 minute of waiting.
我怎样才能在不让 Windows 在一分钟后中止关闭的情况下实现延迟?(我的应用可能需要 2 或 4 分钟才能完成)
How can I achieve a delay without windows aborting the shutdown after a minute? (perhaps it takes 2 or 4 minutes for my app to finish up)
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 完全可以这样做).
Please provide working code (if this is at all possible with windows).
谢谢
推荐答案
嗯,你可以做的一件事就是让你的应用程序在完成必要的工作后触发关闭命令.
Well, one thing you can do is to make your application trigger a shutdown command after finishing the necessary work.
更新:
经过调查,结果发现带有 ENDSESSION
消息的 WndProc
方法被多次触发,导致 CleanUpAndSave()
也被触发再次进行.因此,除了关闭命令之外,您还需要添加一个布尔值来检查消息是否已经从系统发送到您的应用程序.
After investigating this, turns out that the WndProc
method with ENDSESSION
message gets triggered more than once, causing CleanUpAndSave()
to also be carried out again. Hence, in addition to the shutdown command, you'll need to add a boolean to check if the message has already been sent from the system to your application.
以下代码经过测试,在 Windows 7 和 windows 10 上都运行良好:
The following code was tested and it works just fine on both Windows 7 and windows 10:
Private ShutdownDelayed 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
' Check if the message was sent before and the shutdown command is delayed.
If ShutdownDelayed Then Exit Sub
' Block shutdown
ShutdownBlockReasonCreate(Me.Handle, "Testing 123...")
ShutdownDelayed = True
' Do work
CleanUpAndSave()
' Continue with shutdown
ShutdownBlockReasonDestroy(Me.Handle)
' Do shutdown
Dim p As New ProcessStartInfo("shutdown", "/s /t 0")
p.CreateNoWindow = True
p.UseShellExecute = False
Process.Start(p)
' Exit the application to allow shutdown (For some reason 'ShutdownBlockReasonDestroy'
' doesn't really unblock the shutdown command).
Application.Exit()
Return
End If
MyBase.WndProc(aMessage)
End Sub
但是,我建议您不要暂停关闭命令并立即开始做一些工作而不向用户提示确认消息特别是如果这项工作需要几分钟.
However, I suggest that you don't suspend the shutdown command and start doing some work immediately without prompting a confirmation message to the user especially if that work takes several minutes.
相反,您应该向用户显示一个消息框,向用户解释应用程序需要在系统关闭之前做一些工作,并让他们决定是否执行那工作或立即关闭.系统将通知用户您的应用程序正在阻止其关闭.如果他们关心,他们会单击取消"并阅读您的消息.如果他们不在乎,他们仍然可以选择强制关机"无论您是否显示消息.
Instead, you should display a message box explaining to the user that the application needs to do some work before the system shuts down, and let them decide whether to do that work or to shut down immediately. The system will inform the user that your application is preventing it from shutting down. If they care, they will click "Cancel" and read your message. If they don't care, they'll have the option to "Force shutdown" anyway whether you displayed the message or not.
希望有帮助:)
这篇关于Windows 10 关机延迟,VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!