Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to wait for a background thread to finish processing in C#

Tags:

c#

I have a background thread. If the background thread is busy, I want to wait for it to finish its work and then continue with next request. I have implemented it in the following way. Process is the name of the background thread.

 if (process.IsBusy)
 {
     do
     {
         isProcessBusy = process.IsBusy;
     } while (isProcessBusy == false);

     SetIsDirty(status, GetContext());
 }
 else
 {
     SetIsDirty(status, GetContext());
 }

Is this the best way or are there other ways to implement this kind of logic?

like image 578
Rohit Raghuvansi Avatar asked Sep 18 '25 03:09

Rohit Raghuvansi


1 Answers

The Thread class has a method named Join that blocks until the thread on which it is being called exits.
Take a look at here.

like image 194
Itay Karo Avatar answered Sep 20 '25 16:09

Itay Karo