How to keep track of background threads?
I have two threads, one UI thread and one Background thread..
I want to keep a track of the background thread so that I may kill my UI thread (a tray icon) when the background thread has finished...
Please review my code for more details..
Code:
class Program
{
static void Main(string[] args)
{
Thread[] threads = new Thread[2];
threads[0] = new Thread(new ThreadStart(UIMethod));
threads[1] = new Thread(new ThreadStart(BackGroundMethod));
threads[0].SetApartmentState(ApartmentState.STA);
threads[0].Start();
threads[1].Start();
/*What should I write here so that I know when BackGroundMethod has finished, I can kill my system tray icon*/
}
public static void UIMethod()
{
/*Generate the system tray*/
Tray obj = new Tray();
obj.GenerateTrayIcon();
Application.Run();
}
public static void BackGroundMethod()
{
/*Do some heavy background work..*/
}
}
Any suggestions with some code would be highly appreciated..
Thanks.
Re: How to keep track of background threads?
Do you want it to terminate the process when it ends? Then you can simply call Application.Exit().
Re: How to keep track of background threads?
Well, one option is to use a BackgroundWorkerThread to execute the task that the second thread is to fullfill and then when the RunWorkerCompleted event is fired you can close down the app.
Check out the msdn article for it, should give you a good start on it :
http://msdn.microsoft.com/en-us/libr...undworker.aspx