Coolguy
Coolguy

Reputation: 2285

My winform app still running in the background even though I closed the app

Like what the title said, my WinForm app will still runs in the background after I closed the app. I already put the below into my coding:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    Application.Exit();
}

But when I close the app by pressing Alt+F4, then I check the processes inside the task manager, it still running.

Is it that FormClosing() is not run if I close the app by using Alt+F4?

Upvotes: 3

Views: 2710

Answers (1)

Joe Sonderegger
Joe Sonderegger

Reputation: 804

I assume that you have some threads that are still running and are preventing the program from closing.

I use the function to check if the threads are still alive:

  int maxLoops = 100;
  bool oneIsBusy = true;
  while (maxLoops-- > 0 && oneIsBusy)
  {
         oneIsBusy = false;
         Thread.Sleep(100);
         Application.DoEvents();
         foreach (Thread thread in ThreadList)
         {
              oneIsBusy |= thread.IsRunning;
         }
    }

You should keep a log on each thread and then wait for them to close. Application.Exit() should not be called in the FormClosing as it will be called anyway.

Upvotes: 2

Related Questions