Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threads, processes and Application.Exit()

My application consists of main message loop (GUI) and threads (Task.Factory).

In the threads I call some 3rd party applications with var p = new Process();

But when I invoke Application.Exit(); in the message loop - I can see that the processes, that were started in the threads are still in the memory and are being executed.

So the question is - how to kill all the threads and Processes right after Application.Exit(); has been invoked?

UPD:

old:

p.WaitForExit();

new:

while (!p.WaitForExit(1000))
{
    if (FlagToExit)
    {
        p.Kill();
        return;
    }
}
like image 944
zerkms Avatar asked Sep 02 '25 14:09

zerkms


1 Answers

Simply try this

when you create a thread make it background thread

Thread.IsBackground = true;

and on Application.Exit();

all thread will be closed

like image 169
Shrikant Soni Avatar answered Sep 05 '25 03:09

Shrikant Soni