Click here to Skip to main content
14,427,818 members
Rate this:
Please Sign up or sign in to vote.
See more:
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.
Posted
Updated 20-Oct-14 8:09am
v4
Comments
Ziee-M 20-Oct-14 14:32pm
   
I tested the AutoResetEvent on the main thread, and an exception occurs when debuging, I am not sure but it seems the AutoResetEvent.WaitOne() dosen't work in the main thread.


Thank you for your help.

Ps: I removed my answer.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 2

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
   
v2
Comments
[no name] 20-Oct-14 12:03pm
   
Very well explained and good advice, gets my vote of 5.
Sergey Alexandrovich Kryukov 20-Oct-14 12:38pm
   
Thank you very much.
—SA
BillWoodruff 20-Oct-14 12:29pm
   
+5 Perfect answer !
Sergey Alexandrovich Kryukov 20-Oct-14 12:38pm
   
Thank you, Bill.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100