Application.Current.Shutdown() 不会
标题就是关于这个的。 WPF 应用程序,带有一些用于 IPC 的 WCF 内容。我调用 Application.Current.Shutdown()
并且应用程序继续正常运行。我认为 Shutdown
应该是不可阻挡的。
也许是因为它是从后台线程调用的?我需要做一些调度员的摆弄吗?
Title's about it. WPF app with some WCF stuff for IPC. I call Application.Current.Shutdown()
and the app continues on happily. I thought Shutdown
was supposed to be unstoppable.
Perhaps because it's being called from a background thread? Do I need to do some dispatcher fiddling?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当我在主线程以外的任何线程中调用
Application.Current.Shutdown
时,您都会遇到异常,因此我假设您已经正确使用了“调度程序摆弄”。无论如何,这都会编译并退出应用程序,因此如果调度程序位看起来不像您所拥有的那样,您可以将其放入:
You get an exception when I call
Application.Current.Shutdown
in any thread other than the main one, so I'd assume you where using "dispatcher fiddling" properly already.In any case, this compiles and quits an application, so if the dispatcher bit doesn't look like what you have you could sling it in:
根据我的经验,所有线程都必须显式终止或标记为后台线程才能关闭应用程序。
下面是在后台启动读取线程的示例:
IsBackground
属性是关键。如果没有设置,当您调用 Shutdown 时,线程将不会终止。In my experience all threads have to either be terminated explicitly or be marked as background threads in order for the application to close.
Here is an example of kicking off a read thread in the background:
The
IsBackground
property is the key. Without that being set, the thread won't terminate on when you call Shutdown.我仅在从 Visual Studio 运行时遇到
Application.Current.Shutdown
无法工作的情况。在 Visual Studio 中(至少是我使用的 2010 版本)Application.Current.Shutdown
不执行任何操作。如果我单步执行,它会执行这一行,然后继续。如果我从 Windows 资源管理器运行该程序(作为 .exe),则Application.Current.Shutdown
工作正常。可能对此有一个解释,因为在调试期间其他线程处于活动状态,但我无法解释它。
I only experience
Application.Current.Shutdown
not working when I'm running from Visual Studio. Within Visual Studio (at least the 2010 version I'm using)Application.Current.Shutdown
doesn't do a thing. If I single step through it executes this line and then continues. If I run the program (as an .exe) from Windows Explorer thenApplication.Current.Shutdown
works fine.There is probably an explanation for this since during debug other threads are active, but I can't explain it.