App that uses multiple thread remain active after Application.Exit()

T

TomTom

My MainWinForm creates worker thread to do a long processing. Sometimes I
want to close the tool and click the Exit button (Application.Exit()) on
MainWinForm to kill it. However, the tool is still running in the
background, probably because of the worker thread and I have to the Task
Manager to kill the application.

I can work around this problem be using the code below, but I don't think
this is the recommended way. If you have a suggestion, can you let me know?

Process [] processes = Process.GetProcesses();
foreach(Process p in processes)
{
if(p.ProcessName==myProcessName) {
p.Kill();
}

}

Thanks,
TomTom
 
C

Chris Taylor

Hi,

Have you set the threads you are creating to be background threads?
Background threads terminate when the process terminates, foreground threads
prevent the proecess from terminating until the thread has terminated.

The following code demonstrates how to create a background thread.

Thread backgroundThread = new Thread(...);
backgroundThread.IsBackground = true;

backgroundThread.Start();


Hope this helps
 
T

TomTom

Yes, it helped a lot. Now the tool seems to exits cleanly and no processes
are left. Thank you!

Chris Taylor said:
Hi,

Have you set the threads you are creating to be background threads?
Background threads terminate when the process terminates, foreground
threads
prevent the proecess from terminating until the thread has terminated.

The following code demonstrates how to create a background thread.

Thread backgroundThread = new Thread(...);
backgroundThread.IsBackground = true;

backgroundThread.Start();


Hope this helps
--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
TomTom said:
My MainWinForm creates worker thread to do a long processing. Sometimes I
want to close the tool and click the Exit button (Application.Exit()) on
MainWinForm to kill it. However, the tool is still running in the
background, probably because of the worker thread and I have to the Task
Manager to kill the application.

I can work around this problem be using the code below, but I don't think
this is the recommended way. If you have a suggestion, can you let me know?

Process [] processes = Process.GetProcesses();
foreach(Process p in processes)
{
if(p.ProcessName==myProcessName) {
p.Kill();
}

}

Thanks,
TomTom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top