Click to See Complete Forum and Search --> : How to keep track of background threads?


somu0915
May 27th, 2008, 07:55 AM
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..


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.

cilu
May 27th, 2008, 12:21 PM
Do you want it to terminate the process when it ends? Then you can simply call Application.Exit().

trayion
May 27th, 2008, 03:05 PM
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/library/system.componentmodel.backgroundworker.aspx