C# 如何模拟Windows关机进行调试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/520910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:36:16  来源:igfitidea点击:

How to simulate Windows shutdown for debugging?

c#.netwinformsshutdown

提问by Rory

I have an issue with my application when Windows shuts down - my app isn't exiting nicely, resulting in the End Task window being displayed. How can I use the debugger to see what's going on?

当 Windows 关闭时,我的应用程序出现问题 - 我的应用程序没有很好地退出,导致显示“结束任务”窗口。如何使用调试器查看发生了什么?

Is there a way to send the Windows shutdown message(s) to my application so it thinks Windows is shutting down, so I can see exactly how it behaves?

有没有办法将 Windows 关闭消息发送到我的应用程序,以便它认为 Windows 正在关闭,以便我可以确切地看到它的行为?

采纳答案by dkl

There is a tool named Restart Manager (rmtool.exe) in the Microsoft's Logo Testing Tools for Windows, which can be used to send shutdown and restart messages to a process. Logo testing tools can be downloaded here:

Microsoft 的 Windows 徽标测试工具中有一个名为 Restart Manager (rmtool.exe) 的工具,可用于向进程发送关闭和重新启动消息。标志测试工具可以在这里下载:

http://download.microsoft.com/download/d/2/5/d2522ce4-a441-459d-8302-be8f3321823c/LogoToolsv1.0.msi

http://download.microsoft.com/download/d/2/5/d2522ce4-a441-459d-8302-be8f3321823c/LogoToolsv1.0.msi

Then you can simulate shutdown for your process:

然后您可以为您的流程模拟关机:

rmtool.exe -p [PID] -S

where [PID] is the process ID. According to the Vista Logo Certification Test Cases document,

其中 [PID] 是进程 ID。根据 Vista Logo Certification Test Cases 文档,

Restart Manager shutdown messages are:

a. WM_QUERYENDSESSION with LPARAM = ENDSESSION_CLOSEAPP(0x1): GUI applications must respond (TRUE) immediately to prepare for a restart.

b. WM_ENDSESSION with LPARAM = ENDSESSION_CLOSEAPP(0x1): The application must shutdown within 5 seconds (20 seconds for services).

c. CTRL_SHUTDOWN_EVENT: Console applications must shutdown immediately.

重新启动管理器关闭消息是:

一种。WM_QUERYENDSESSION with LPARAM = ENDSESSION_CLOSEAPP(0x1):GUI 应用程序必须立即响应 (TRUE) 以准备重新启动。

湾 WM_ENDSESSION with LPARAM = ENDSESSION_CLOSEAPP(0x1):应用程序必须在 5 秒内关闭(服务为 20 秒)。

C。CTRL_SHUTDOWN_EVENT:控制台应用程序必须立即关闭。

回答by Jon Tackabury

I believe when Windows is shutting down it sends a "WM_QueryEndSession" to all applications. To simulate a Windows shutdown you could create a little application that just does a PostMessage with this message to your application and see what happens. Windows may send more messages than that to actually close your application (like WM_CLOSE), but whenever your application receives the "WM_QueryEndSession" message it means your application is about to have the rug pulled out from under it.

我相信当 Windows 关闭时,它会向所有应用程序发送“WM_QueryEndSession”。要模拟 Windows 关机,您可以创建一个小应用程序,该应用程序只向您的应用程序发送带有此消息的 PostMessage,然后看看会发生什么。Windows 可能会发送比实际关闭应用程序更多的消息(如 WM_CLOSE),但是每当您的应用程序收到“WM_QueryEndSession”消息时,就意味着您的应用程序即将从它下面拉出地毯。

回答by jamesmillerio

You could use the SystemEvents.SessionEndingevent, which is fired when a user logs off or shuts down. Be careful when using it though, some resources are not guaranteed to be available. For example, my application needed to hit a server when it was shutting down to clock a user out (a timeclock application), but the network card is sometimes already disabled when this event occurs. Since you're just doing cleanup, this should work fine.

您可以使用SystemEvents.SessionEnding事件,该事件在用户注销或关闭时触发。使用时要小心,有些资源不能保证可用。例如,我的应用程序需要在关闭时访问服务器以使用户退出(时钟应用程序),但是当发生此事件时,网卡有时已经被禁用。由于您只是在进行清理,因此这应该可以正常工作。

回答by Rami A.

SendMessagecan be used to send window messages with any parameters to a window.

SendMessage可用于向窗口发送带有任何参数的窗口消息。

Very useful for debugging and testing.

对于调试和测试非常有用。

  1. Send the message WM_QUERYENDSESSIONwith LPARAM= ENDSESSION_CLOSEAPP. The application must return 1 (TRUE) to indicate it's prepared to shut down and restart.

  2. Send the message WM_ENDSESSIONwith LPARAM = ENDSESSION_CLOSEAPPThe application must shut down within the specified timeout period.

  1. WM_QUERYENDSESSION使用LPARAM=发送消息ENDSESSION_CLOSEAPP。应用程序必须返回 1 (TRUE) 以指示它已准备好关闭和重新启动。

  2. 发送WM_ENDSESSION带有 LPARAM =ENDSESSION_CLOSEAPP应用程序必须在指定的超时期限内关闭的消息。