主线程等待直到子线程中止 [英] main thread wait untill a child thread aborted

查看:141
本文介绍了主线程等待直到子线程中止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个子线程来做这种代码



  true 
{
if (flag) // 应用程序正在关闭
{
/ / 中止子线程
}
else
// do somthing
// 这部分代码必须一直运行到最后或不开始。
// 子线程不应该在这里中止

}




$应用程序关闭时b $ b我需要中止子线程以释放对文件的所有访问权限,因此在表单关闭事件中我转fla g为真。主线程应该等到子线程自行中止。

当我使用child.Join();在主线程中。表单变得冻结。

当我设置child.IsBackground = true;它终止于不安全的地方。

解决方案

问题是:你不知道你想要什么。对 Thread.Join()的调用是阻塞的,因此调用者代码应该冻结;这是等待概念的本质。不要叫这个方法。在这种情况下,你不是在等待线程被终止,但你不能等待而不是在同一时间等待。



很可能你想要不同的东西。当然,您不希望UI冻结。但是你会想要什么呢?当然,不是等待。您可能希望通知UI线程线程 child 已完成,并在此通知的UI中执行某些操作,例如,启用一些禁用按钮。然后做到这一点。



你提到线程正在中止。然后你可以处理 ThreadAbortException 在处理程序中发送通知。这是通过使用一些委托实例调用 Control.Invoke 来完成的。此代码可以在任何线程中执行,并且旨在用于非UI线程。此调用将将调用委托实例所需的数据发布到UI线程事件队列,因此最终将在UI线程中调用委托实例。请参阅:

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invoke%28v=vs.110%29.aspx [ ^ ]。



有时候,在终止的线程中发送这样的通知是不合适的,因为它可能以不同的方式发生,等等。您只需要在线程执行停止的时刻捕获,无论出于何种原因,这不是您关心的。然后可能的方法是:你可以创建第三个线程,等待你的线程被终止,然后,等待后,它通知用户界面。



-SA

i have a child thread that do this kind of code

while(true)
{
   if(flag)//application is closing
   {
     //abort the child thread
   }
   else 
   //do somthing
   //this part of code must down till the end or do not start.
   //child thread should not aborted here

}



when application is closing i need to abort the child thread to release all access to files so in form close event i turn "flag" to true. main thread should wait till child thread aborts itself.
when i use child.Join(); in main thread . form becomes freezed.
when i set child.IsBackground = true; it terminated in unsafe place.

解决方案

The problem is: you don't know what you want. This call to Thread.Join() is blocking, so the caller code is supposed to "freeze"; this is the essence of the concept of "waiting". Don't call this method. In this case, you are not waiting for the thread to be terminated, but you cannot want to wait and not to wait at the same time.

It's likely that you want something different. Of course, you don't want the UI to freeze. But what would you possibly want then? Of course, not "waiting". Probably, you want to notify the UI thread that the thread child is finished and do something in UI on this notification, for example, enable some disable buttons. Then do exactly that.

You mentioned that the thread is being aborted. Then you can handle ThreadAbortException send a notification in the handler. This is done by calling Control.Invoke with some delegate instance. This code can be executed in any thread, and is intended to be used in a non-UI thread. This call will post the data needed to invoke a delegate instance to the UI thread event queue, so the delegate instance will ultimately be invoked in the UI thread. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invoke%28v=vs.110%29.aspx[^].

Sometime, it is unsuitable to send such notification in the thread being terminate, because it can happen in different ways, and so on. You only need the capture the moment of time when the thread execution is stopped, by whatever reason, which is not what you care about. Then the possible approach is this: you can create 3rd thread which waits for your thread to be terminated and then, after the wait, it notifies the UI.

—SA


这篇关于主线程等待直到子线程中止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆