如何避免WPF应用程序注销时没有响应应用程序、WPF

2023-09-09 21:12:11 作者:英雄♀奶油♂

当试图preSS标准的Windows 7注销按钮的同时,我的WPF应用程序正在运行,我得到这个程序是preventing Windows无法注销。我想强行关闭,而不必preSS强制注销。

When trying to press the standard Windows 7 logoff button while my WPF app is running, I get "This program is preventing Windows from logging off". I would like to force it to close without having to press "Force log off".

同样,pressing结束任务的(应用)任务管理器使其成为无响应,而不仅仅是关闭程序。

Similarly, pressing "End Task" in the (applications) Task Manager causes it to become non-responsive rather than just close the program.

我已经尝试添加这WINDOW_CLOSING,但是这似乎并没有做到这一点:

I have tried adding this to Window_Closing, but this doesn't seem to do it:

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    Environment.Exit(0);
}

我新的WPF,但在我看来,该窗口没有正常关闭。我怎样才能prevent的计划是preventing Windows无法注销当执行Windows的注销,或程序没有响应从任务管理器杀死的时候吗?

I'm new to WPF, but it seems to me that the window is not closing properly. How can I prevent the "program is preventing Windows from logging off" when executing the Windows logoff, or "Program is not responding" when killing it from task manager?

推荐答案

这应该只是一个问题,如果你的应用程序没有响应从Windows发送的关闭事件。

This should only be an issue if your application is not responding to the close events sent from Windows.

如果您正在执行code中的UI线程,其起价响应消息p $ pvents它这通常发生。因此,把东西在合闸事件和类似的不会有任何影响,因为他们将无法处理,直到你的工作结束后。

This will typically happen if you are executing code on the UI thread, which prevents it from responding to messages. As such, putting something in the closing events and similar will have no effect, as they won't be able to process until after your "work" finishes.

正确的方式来处理,这是对您的工作转移到后台线程。这样可以使应用程序响应,这使得它能够从Windows到关闭请求进行响应。

The proper way to handle this is to move your work onto background threads. This keeps the application responsive, which allows it to respond to the request from Windows to Close.