none
Exit Application and application stops and does not dispose RRS feed

  • Question

  • Hello everyone,

     

    I got a problem in C#: in my application there are 2 serial ports communication and when I click on a button to exit whole application, I expect that application would exit but my case is the application stops without disposing: this is my code for event of button:

     

    Code Snippet

    private void Button_Exit_Application_Click(object sender, EventArgs e)

    {

       try

       {

         timer_Communication.Stop();

         System.Threading.Thread.Sleep(10);

         if (comport.IsOpen)

         {

           comport.DiscardInBuffer();

           comport.DiscardOutBuffer();

           comport.Close();

           comport.Dispose();

         }

        if (readerPort.IsOpen)

        {

           readerPort.DiscardInBuffer();

           readerPort.DiscardOutBuffer();

           readerPort.Close();

           readerPort.Dispose();

         }

         runApplication.Close();

         Application.Exit();

       }

       catch (Exception ex)

       {

          MessageBox.Show("Bug 019\n" + ex.Message);

       }

    }

     

     

     

    I event try to stop port communication before closing the application but the problem still occurs. Could anyone know any solution, please help me?

     

    Thank you very much,

     

    Regards,

     

    Joesy

    Tuesday, July 24, 2007 1:46 PM

Answers

  • You're forcing your application to exit abormally, circumventing any disposal.

     

    I think you should be asking why your Form doesn't close properly.  If you just Close your for you'll get what you're after.

    Wednesday, July 25, 2007 8:05 PM

All replies

  • Try tying it into Application.ApplicationExit

     


             

    Code Snippet

    Application.ApplicationExit += new EventHandler(Application_ApplicationExit);

     

    void Application_ApplicationExit(object sender, EventArgs e)
     {
        while(comport != null)
        {
            comport.Dispose();
            comport = null;
        }

     

       while(readerport != null)

       {

           readerport.Dispose();

           readerport = null;

       }
     }

     

     

    Tuesday, July 24, 2007 3:02 PM
  • Dear Tomas Scheel,

     

    It does not work, I even try this:

     

    Code Snippet

    private void Application_ApplicationExit(object sender, EventArgs e)

    {

    try

    {

    timer_Setting.Stop();

    timer_Communication.Stop();

    System.Threading.Thread.Sleep(15);

    while (comport != null)

    {

    comport.Dispose();

    comport = null;

    }

    while (readerPort != null)

    {

    readerPort.Dispose();

    readerPort = null;

    }

    System.Threading.Thread.Sleep(15);

    runApplication.Close();

    Application.Exit();

    }

    catch (Exception ex) { MessageBox.Show("Bug 019\n" + ex.Message); }

    }

     

    private void Button_Exit_Application_Click(object sender, EventArgs e)

    {

    Application.ApplicationExit += new EventHandler(Application_ApplicationExit);

    Application.ExitThread();

    Application.Exit();

    }

     

     

    Thanks anyway,

     

    Joesy

    Wednesday, July 25, 2007 9:28 AM
  • What isn't being disposed, exactly? What are the symptoms? How are you testing?

    Wednesday, July 25, 2007 9:33 AM
  • Hey Matthew Watson,

     

    Thanks for you reply,

     

    I mentioned on the first port, I want to click on a button and the application must exit completely. but my case is that I click on the button and the form could not be closed. I think because of the communication port is running and I tried to stop it then dispose it. but it seems the problem is till there.

     

    Thanks anyway,

     

    Joesy

    Wednesday, July 25, 2007 3:02 PM
  • You're forcing your application to exit abormally, circumventing any disposal.

     

    I think you should be asking why your Form doesn't close properly.  If you just Close your for you'll get what you're after.

    Wednesday, July 25, 2007 8:05 PM
  • You're forcing your application to exit abormally, circumventing any disposal.

    I wonder where you got this conclusion from?

    Maybe in 2007 the case was different, but atm the docs for Application.Exit say:

    "Exit raises the following events and performs the associated conditional actions: A FormClosing event is raised for every form represented by the OpenForms property..."
    Friday, May 12, 2023 12:21 PM