views:

115

answers:

1

When i used

  foreach (Form form in Application.OpenForms)
    {

        form.Close();
    }
    Application.Exit();

It not exit Application?

How can I exit the application making sure all running thread are closed?

+4  A: 

You don't show the use of any threads in your code, but let's suppose you do have threads in it. To close all your threads you should set all of them to background threads before you start them, then they will be closed automatically when the application exits, e.g.:

Thread myThread = new Thread(...);
myThread.IsBackground = true; // <-- Set your thread to background
myThread.Start(...);

A "HOWTO: Stop Multiple Threads" article from microsoft: http://msdn.microsoft.com/en-us/library/aa457093.aspx

Lirik
+1 but don't overdo it. "IsBackground" is enough IMO :)
Oskar Kjellin
@Oskar OK :)... I'll just leave the reference.
Lirik