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

  • Thread starter Thread starter TomTom
  • Start date Start date
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
 
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
 
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
 
Back
Top