Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
if it directly exits, how to make a thread wait for event handler to finish.

What I have tried:

tried diferent ways to block thread by sleeping, wait for exit etc... but nothing seemed to be the right way.
Posted
Updated 29-Mar-16 7:51am
Comments
Jochen Arndt 29-Mar-16 3:43am    
You should show as an example of your thread code.

If you use an event to exit the thread and your thread may terminate also due to other reasons, you can call Thread.Join() after firing the event. If the thread has already terminated when firing the event, Join() will return immediately. Otherwise, Join() will wait until the thread has terminated.
Sergey Alexandrovich Kryukov 29-Mar-16 9:56am    
Nothing happens. It continues to exist. You have to explain what the problem is. What is "active event handler"?
Anyway, in my answer, I explained how to wait properly. In another thread, you can set (put to signaled state) some wait handler another thread could wait for. This setting can be don in an event handler, but it has nothing to do with the waiting thread and its event instances. Perhaps you have some misconception, but main problem is that you did not explain properly what you want to achieve.
—SA

You have to use an event wait handle object. No way you should do Sleep, any spin wait, nothing like that. If you wait for some object, your thread goes to a wait state: it is switched off and not scheduled back to execution until it is waken up, spending zero CPU time in wait state. You can also wait for a thread.

Please see my past answers:
pause running thread[^],
Running exactly one job/thread/process in webservice that will never get terminated (asp.net)[^],
Making Code Thread Safe[^],
ManualResetEvent and AutoResetEvent in Thread[^].

To wait for some thread to finish, also in a wait state, you can use System.Threading.Thread.Join:
Thread.Join Method (System.Threading)[^].

—SA
 
Share this answer
 
v2
Comments
[no name] 29-Mar-16 9:58am    
That looks fine, a 5.
Sergey Alexandrovich Kryukov 29-Mar-16 14:21pm    
Thank you, Bruno.
—SA

I feel that using the relatively new async feature of C# is the best way to implement asynchronous methods because it provides an exit point for the method when it completes. You can use it like this


C#
//subscribe to the event
button.Click += MyEventHandlerAsync;
// When the event fires this handler is called
private async void MyEventHandlerAsync(object sender, RoutedEventArgs e)
       {
           //the UI  thread starts the task here and then returns
           //the task runs on the threadpool
           string text = await Task.Run(() => this.MyMethodThatReturnsAString());
           //When the task finishes, control returns here
           //Do something with the result
           //Set a label on the UI thread
           this.label.Content = text;
       }

       private string MyMethodThatReturnsAString()
       {
           //Long running method that finds the meaning of life
           //........
           return "Forty Two ";
       }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900