locked
Best Practice for Application.Exit() RRS feed

  • Question

  • Hi.
    Environment:
    Device: HP iPAQ 110 Classic Handheld
    OS: Windows Mobile 6 Classic
    Framework: .NET Compact Framework 3.5

    What is best practice with respect to Application.Exit(). That is, should it be called and if so where? Is it ok to just call Close()?  This is what I am planning to do:
    /// <summary>
    /// Called to exit the application. First Close() is called then, Application.Exit(). Hopefully, this will force some stuborn threads to exit (usually takes up to 30 seconds or more).
    /// </summary>
    private void CloseApplication()
    {
      try
      {
        Close();
      }
      catch (Exception e)
      {
        MyLogger.LogErrorLine(e);
      }
      finally
      {
        Application.Exit();
      }
    }
    
    /// <summary>
    /// Released Resources 
    /// </summary>
    protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
    {
      try
      {
        MyDisposer(ref timer, timer_Tick);
        base.OnClosing(e);
      }
      catch (Exception e1)
      {
        MyLogger.LogErrorLine(e);
    #if DEBUG
        MyMessageBox.ShowError("Error Quiting Program");
    #endif
      }  
    }
    Currently, I just call Close() however, I notice that certain threads take a while, 30seconds or more, to exit Franson Bluetools and, mostly, a dll that I wrote to monitor power status changes.

    Finally, I would be appreciative if you pay attention to the Form's constructor, Form1:
    /// <summary>
    /// Constructor, please note that CloseApplication() is invoked to exit Application should any exceptions occur!.
    /// </summary>
    public Form1()
    {
      InitializeComponent();
      try
      {
        SerialMediator.Initialize(this);
        MyDataAdapter.Initialize(this);
      }
      catch (Exception e)
      {
        MyLogger.LogErrorLine(e, "Initialization Failed");
        MyMessageBox.ShowError("Error Starting Program");
        // !!!!!!! NOTE CALL OF CloseApplication() from withong Form1() Ctor !!!!!!!!!
        CloseApplication();
      }
    }
    Here the CloseApplication() method is invoked if exception occurs however, is this safe ok to do in a constructor, given that CloseApplication() calls Close() and Application.Exit()?
    Wednesday, September 9, 2009 7:18 PM

Answers

  • The best practice is to use Form.Close() instead of Application.Exit().

    When the last form is closed (usually the main one, You initialize in Program.cs) application will terminate on its own.
    If You'll catch an error, program will terminate on its own too, by simply going to what's next after Application.Run(new Form()). If there's nothing there, app will terminate and will probably show the error unless You caught the exception.

    Everything, nice and easy as it should be.
    There's no need of using Application.Exit(). If someone's using it, it means that he's a begginer in C# programming.

    If You'll find my answer satisfactory or helpful - mark it as answered or vote for it! Thank You.
    "If You think You know better then me, why is Your code not working, then don't waste my time at this forum. Otherwise - do as I'm suggesting."
    • Proposed as answer by Mal Loth Wednesday, September 9, 2009 8:15 PM
    • Marked as answer by ZHE ZHAO Wednesday, September 16, 2009 1:39 AM
    Wednesday, September 9, 2009 8:14 PM
  • Hi,

    The following blog post adds a little more to Mal Loth's reply :-

    http://blogs.msdn.com/tom_krueger/archive/2005/02/24/379678.aspx

    Thanks

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    • Marked as answer by ZHE ZHAO Wednesday, September 16, 2009 1:39 AM
    Wednesday, September 9, 2009 9:10 PM

All replies

  • The best practice is to use Form.Close() instead of Application.Exit().

    When the last form is closed (usually the main one, You initialize in Program.cs) application will terminate on its own.
    If You'll catch an error, program will terminate on its own too, by simply going to what's next after Application.Run(new Form()). If there's nothing there, app will terminate and will probably show the error unless You caught the exception.

    Everything, nice and easy as it should be.
    There's no need of using Application.Exit(). If someone's using it, it means that he's a begginer in C# programming.

    If You'll find my answer satisfactory or helpful - mark it as answered or vote for it! Thank You.
    "If You think You know better then me, why is Your code not working, then don't waste my time at this forum. Otherwise - do as I'm suggesting."
    • Proposed as answer by Mal Loth Wednesday, September 9, 2009 8:15 PM
    • Marked as answer by ZHE ZHAO Wednesday, September 16, 2009 1:39 AM
    Wednesday, September 9, 2009 8:14 PM
  • Hi,

    The following blog post adds a little more to Mal Loth's reply :-

    http://blogs.msdn.com/tom_krueger/archive/2005/02/24/379678.aspx

    Thanks

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    • Marked as answer by ZHE ZHAO Wednesday, September 16, 2009 1:39 AM
    Wednesday, September 9, 2009 9:10 PM
  • Ok, then perhaps there is other reason why it takes threads so long to exit. 
    Friday, September 11, 2009 2:12 PM