I have a WinForms application that consists of a main UI thread and 4 tasks. My main form has a member level variable like this:
private bool keepThreadsRunning = false;
In the Load () event of my main form, I have the following:
keepThreadsRunning = true;
var task1Worker = Task.Factory.StartNew(() => DoStuff1());
var task2Worker = Task.Factory.StartNew(() => DoStuff2());
var task3Worker = Task.Factory.StartNew(() => DoStuff3());
var task4Worker = Task.Factory.StartNew(() => DoStuff4());
Inside each of my DoStuff () methods, I basically have this:
while (keepThreadsRunning)
{
Thread.Sleep(30000);
}
Finally, in my Form_Closing () event handler, I have the following:
keepThreadsRunning = false;
this.Close();
Watching my application in the task manager, it seems that the process ends when I close the form, but I got a little confused in the four tasks. Is my call to this.Close () really causing these tasks to complete (even if they are in the Thread.Sleep () call when this happens)? And is there a better way to do this than the way I code it now?
EDIT. ( ), , , , . , 30 , , 30s-wait ( Thread.Sleep()), .