none
C# application.exit not working when e.cancel is true RRS feed

  • Question

  • Hi All,

    I have create a application "Windows Keylogger Tool". What I am doing is when someone click on close button of applicaiton what I do is

    e.cancel=true
    and hide the applicaiton

    works fine.

    But from application's exit button. When someone click on that application should exit.

    Application.Exit();

    I did this. Application suppose to exit. But when I see in task manager application still running.

    And when I try to run exe again. It will give error.

    The same window 7 error dailog box.

    Check online for solution
    Close the program
    Debug the program

    Help me.

    Saturday, May 21, 2011 12:46 PM

Answers

  • I'm not sure how you coded your test but this works just fine:

    using System;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
        }
    
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
          Console.WriteLine("FormClosed");
        }
    
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
          Console.WriteLine("FormClosing");
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          Application.Exit();
        }
      }
    }
    
    Both events get fired when I click the button.  I also tried it with a second form open and that form also fires its events.

    ShaneB

    • Proposed as answer by Paul Zhou Monday, May 23, 2011 9:05 AM
    • Marked as answer by Paul Zhou Monday, May 30, 2011 7:26 AM
    Sunday, May 22, 2011 8:56 PM

All replies

  • This method (Application.Exit) stops all running message loops on all threads and closes all windows of the application. This method does not force the application to exit.

    The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.


    Make everything as simple as possible, but not simpler.
    Saturday, May 21, 2011 11:36 PM
  • That's incorrect.  Form.Closing and Form.Closed are raised on all open forms when you use Application.Exit.  The problem is that he's canceling the main form's close which is preventing the application from exiting.

    Shyam, try this instead in your form's closing event:

    if (e.CloseReason == CloseReason.UserClosing)
    {
     e.Cancel = true;
    // hide application code
     }
    HTH

    ShaneB

    Sunday, May 22, 2011 1:46 AM
  • Shyam,

    You can do as ShaneB said, or here is another solution:

    use Environment.Exit(0); instead of Application.Exit(); at the Exit button.

     

    Regards,

    Jimmy.


    Wassouf
    Sunday, May 22, 2011 8:54 AM
  • What's the best way to close an app?

    System.Diagnostics.Process.GetCurrentProcess().Kill().

     

    This works always.

    • Proposed as answer by Jacob Brown Sunday, May 22, 2011 12:53 PM
    • Unproposed as answer by Jacob Brown Sunday, May 22, 2011 2:14 PM
    • Proposed as answer by Jacob Brown Monday, May 23, 2011 11:42 AM
    Sunday, May 22, 2011 12:53 PM
  • Yes, that's the best way if you want to terminate an app...abruptly.  I don't think that's what he's looking for.  The solution I posted is for a graceful shutdown where all normal event handlers will be called.

    ShaneB

    • Proposed as answer by Jacob Brown Monday, May 23, 2011 11:42 AM
    Sunday, May 22, 2011 1:29 PM
  • Just kidding.

    In my opinion I would add a function in a separate file for example: ApplicationQuitting.cs. And all the FormClosing and FormClosed events would lead their.

    The e paramether would be sent by reference, so the ApplicationQuitting function would be able to stop the quitting stack.

     

     

    • Proposed as answer by Jacob Brown Monday, May 23, 2011 11:42 AM
    Sunday, May 22, 2011 2:17 PM
  • That's incorrect.  Form.Closing and Form.Closed are raised on all open forms when you use Application.Exit.  The problem is that he's canceling the main form's close which is preventing the application from exiting.

    Shyam, try this instead in your form's closing event:

    if (e.CloseReason == CloseReason.UserClosing)
    {
     e.Cancel = true;
    // hide application code
     }
    HTH

    ShaneB

    I advise you to read the Application.Exit, or you can try it. Form.Closing event is not raised when the Application.Exit method is called to exit your application.

    Also Environment.Exit is for only the "console applications".


    Make everything as simple as possible, but not simpler.
    Sunday, May 22, 2011 8:35 PM
  • That's for version 1.1 of the .Net framework...which is very old.  As of version 2.0, Form.Closed and Form.Closing are in fact raised when the user calls Application.Exit. As stated on MSDN:  "Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed."  When it posts the WM_CLOSE message to the forms, they fire their events.

    Test it yourself if you don't believe me...I did before posting.  And I never recommended anyone use Environment.Exit.

    ShaneB

    Sunday, May 22, 2011 8:49 PM
  • No, I'm trying on Framework 4.0, I did test it, the events are not raised.
    Make everything as simple as possible, but not simpler.
    Sunday, May 22, 2011 8:52 PM
  • I'm not sure how you coded your test but this works just fine:

    using System;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
        }
    
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
          Console.WriteLine("FormClosed");
        }
    
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
          Console.WriteLine("FormClosing");
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          Application.Exit();
        }
      }
    }
    
    Both events get fired when I click the button.  I also tried it with a second form open and that form also fires its events.

    ShaneB

    • Proposed as answer by Paul Zhou Monday, May 23, 2011 9:05 AM
    • Marked as answer by Paul Zhou Monday, May 30, 2011 7:26 AM
    Sunday, May 22, 2011 8:56 PM
  • I'm confused, I'm testing with code below:

    public partial class Form1 : Form
    {
      public Form1()
      {
        InitializeComponent();
      }
    
      private void button1_Click(object sender, EventArgs e)
      {
        Application.Exit();
      }
    
      protected override void OnClosing(CancelEventArgs e)
      {
        MessageBox.Show("onclosing");
        base.OnClosing(e);
      }
    }
    


    Make everything as simple as possible, but not simpler.
    • Proposed as answer by Jacob Brown Monday, May 23, 2011 11:43 AM
    Sunday, May 22, 2011 9:00 PM
  • From MSDN: "The OnClosing method is obsolete in the .NET Framework version 2.0; use the OnFormClosing method instead.".  Of course, it should read 2.0 and above.  If you were to override OnFormClosing instead, it would work.

    There's the problem :)

    ShaneB



    • Edited by Shane_B Sunday, May 22, 2011 9:04 PM added last part
    • Proposed as answer by Jacob Brown Monday, May 23, 2011 11:43 AM
    Sunday, May 22, 2011 9:02 PM
  • Yes, I found too. I think the conversation is helpful to for this topic :) Thank you.
    Make everything as simple as possible, but not simpler.
    • Proposed as answer by Jacob Brown Monday, May 23, 2011 11:43 AM
    Sunday, May 22, 2011 9:08 PM
  • I don't want any misunderstanding about that:

    Form.Closed, Form.Closing method are can be obsolete, but it still can use. What I want to say is if you are using them- you should call the Form.Close method for each open form individually before calling the Exit method.

    The information that I gave in my first post is not incorrect.

    Make everything as simple as possible, but not simpler.

    Sunday, May 29, 2011 12:58 AM
  • I can agree with that.  I should have said Form.FormClosed and Form.FormClosing in my earlier posts...even I got confused by the simililarity in the event names :)

    Still, using obsolete methods is not a good practice anyway.  If he uses the code I initially posted in the Form.FormClosing event it will resolve his issue without having to call Close() on every open form. Of course, the original poster hasn't even replied so he probably didn't even see it.

    Good discussion anyway!

    ShaneB

    Sunday, May 29, 2011 1:16 AM
  • Just use Environment.Exit (0); that doest not fires the close validation events.

    Works fine for me, allowing confirm if close form and when the confirm is true but other windows has the same validation no problem. and if that confirm is true and call Environment.Exit (0) other FormClosing events are not fired and the application closes gracefully.

    Sunday, September 30, 2012 5:00 AM
  • This worked for me.  As the "e.Cancel = true" in my (perpetually open) form was preventing the application from closing.  Good call.
    Thursday, October 18, 2012 12:11 AM