views:

1504

answers:

3

Hello everyone,

I am using VSTS 2008 + .Net 3.5 + C# to develop Windows Forms application. My confusion is, seems Application.Exit does not force application to terminate? If not, which method should I call to make application terminate?

EDIT 1:

Normally the main method is like this, how to exit Main function gracefully without calling Environment.Exit?

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        try
        {
            Application.Run(new Form1());
        }
        catch (Exception ex)
        {
            Console.WriteLine (ex.Message);
        }
    }

thanks in advance, George

+10  A: 

Application.Exit really just asks the message loop very gently.

If you want your app to exit, the best way is to gracefully make it out of Main, and cleanly close any additional non-background threads.

If you want to be brutal... Environment.Exit or Environment.FailFast? note this is harsh - about the same as killing your own Process.

Marc Gravell
+1 for great personification
colithium
Thanks Marc, I am confused how to exit Main function gracefully. I have posted my code in my original post under EDIT 1 section. Any ideas why?
George2
Marc, I read your reply, but where to call Close? Inside Main or replace the code to call Application.Exit with code to call Close?
George2
Either ;-p Presumably, though, you are in the form when you want to exit (perhaps as a button etc) - so the form can issue this.Close() to itself.
Marc Gravell
Thanks Marc, 1. I am interested to learn what does Application.Exit really do -- just make message pump stops and no more function (like terminate application)? 2. If Application.Exit does not have function to terminate Windows Forms application, why if we set other thread's property to backgourd threads, after calling Application.Exit will terminate the application?
George2
It is a bit like using Close from the task-bar... http://msdn.microsoft.com/en-us/library/ms157894.aspx; it asks the message loops to commit suicide, but this can be cancelled, and doesn't account for other threads.
Marc Gravell
+2  A: 

If your application does not exit gracefully when you call Application.Exit there is (obviously) something that prevents it from doing so. This can be anything from a form setting e.Cancel = true in the FormClosing event, to a thread that is not a background thread that is still running. I would advice you to carefully research exactly what it is that keeps your process alive, and close that in a nice manner. That should make your application close nicely as well.

Typically, in a winforms application, it should be sufficient to close the main form.

Fredrik Mörk
You mean if all threads I created explicitly are marked as background, then application should exit after calling Application.Exit?
George2
@George2: at least they will not keep the application alive by being foreground threads; there may be other reasons why it does not exit, but that is one place to check. I do think though that the the default value for Thread.IsBackground is false. Worth checking though.
Fredrik Mörk
Thanks @Fredrik, when I call Application.Exit, there are some non-backgroud threads running (they are created by myself explicitly using new Thread().Start()), do you think it is the reason of the explicitly created threads which blocks my application from exiting (when call Application.Exit())?
George2
@George2: impossible for me to say, but if I was sitting with the code, and with the information that I have right now, that would be my first bet.
Fredrik Mörk
If that is true, consider adding some signaling mechanisms (EventWaitHandle) to notify those threads that they should end. Then join them and wait until they are finished. (That's better than making them background and killing them automatically)
Groo
Thanks Fredrik, "that would be my first bet" -- you mean set the threads which I create explicitly as backkground ones?
George2
@Groo, good solution, 1. do you think the root cause of my issue -- Forms application does not terminate after calling Application.Exit is because of the threads I created expliicitly is not terminated?2. If yes, I am interested to learn what does Application.Exit really do -- just make message pump stops and no more function?
George2
+1  A: 

I use

if (System.Windows.Forms.Application.MessageLoop)
{
   // Use this since we are a WinForms app
   System.Windows.Forms.Application.Exit();
}
else
{
   // Use this since we are a console app
   System.Environment.Exit(1);
}

from http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx

Peter Gfader
Thanks Peter, 1. I am interested to learn what does Application.Exit really do -- just make message pump stops and no more function (like terminate application)? 2. If Application.Exit does not have function to terminate Windows Forms application, why if we set other thread's property to backgourd threads, after calling Application.Exit will terminate the application?
George2
Hi George2, from MSDN: 1. Application.Exit Informs all message pumps that they must terminate, and then *closes all application windows* after the messages have been processed.2. it has the function to terminate/close
Peter Gfader