本文介绍了Thread.Join在WPF中关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短版本:如何在WPF应用程序中执行thread.Join()而不会死锁UI?

Short version: How do I do thread.Join() in a WPF app without deadlocking the UI?

在Window_Closing()期间,此应用程序关闭了各种线程.如果GUI调用.Join,则UI会挂起并在调用Dispatch.Invoke()时立即陷入死锁.当线程尝试更新用户界面以指示时会发生这种情况 它被中止了.

During the Window_Closing() this application shuts down various threads.  If the GUI calls .Join on them then the UI hangs and gets a deadlock as soon as something calls Dispatch.Invoke().  This happens when a thread tries to update the UI to indicate it was aborted.

我需要的是类似MsgWaitForMultipleObjects()的东西,但使用WPF的方式.我什至不确定在Window_Closing()事件期间是否安全或是否可以处理消息,这一事实使情况变得复杂.

What I need is something like the old MsgWaitForMultipleObjects() but in WPF fashion.  It is complicated by the fact that I'm not even sure it is safe or possible to process messages during a Window_Closing() event.

我在寻找想法...

推荐答案

如果您只想在UI线程退出时中止线程,那么我认为在创建线程时仅在线程上设置IsBackground就足够了.当UI线程退出时,它们会自动中止.这样,您的应用可能会终止.

If you just want to abort threads when UI thread exits then I think that just setting IsBackground on the thread when create them should be enough. They automatically get aborted when UI thread exits. In this way, your app may be terminated.

http://msdn.microsoft.com/en-us/library /system.threading.thread.isbackground.aspx

谢谢,
穆罕默德

Thanks,
Muhammad


这篇关于Thread.Join在WPF中关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:03