Click to See Complete Forum and Search --> : [RESOLVED] Threads listing and general use


Otex
February 7th, 2009, 10:43 PM
I need to add thread to various things in an application I am working on and I have a bit of difficulty with the architecture and control of the threads.

I will have a few questions, all on threads, lets start simple

I have a main form which needs to spawn a second form when we click a button but I need to still be able to use the parent form.
I have something like.

private void button4_Click(object sender, EventArgs e)
{
Thread threadErrorPanel = new Thread(new ThreadStart(ErrorPanelStart));
threadErrorPanel.Start();

}
private void ErrorPanelStart()
{
ErrorPanel fm2 = new ErrorPanel();
fm2.ShowDialog();
}


It works fine, the two problems are:
a. If I click on the button again it lanches a 3rd form etc..
-- How can I check if a thread is already running ?

b. if I close the Main form the 2nd form is still running
-- How can I make main close all the threads that have been started ?

c. How can I list all the threads launched from my application?
Do I have to create a hashtable and populate it with my threads as I start them ? If so how can I maintain the table when a thread abnormally aborts ?

Thanks

code?
February 8th, 2009, 04:29 PM
You would probably have to have a list designated for threads, then on application exit -> loop through the list and close all threads.

toraj58
February 9th, 2009, 11:40 AM
by default threads are forground and will not be closed if you exit the application you need set IsBackground property of the threads to ture then if you exit the application they all be ceased to exist.

Otex
February 15th, 2009, 01:00 AM
Thanks for answers, I made it keeping the thread in a list.