Application.Exit() Doesn't close.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Application.Exit() Doesn't close.

  1. #1
    Join Date
    Jan 2009
    Posts
    2

    Application.Exit() Doesn't close.

    Hey,

    I've got a problem when I try to close an application.

    I've got several forms and when the application loads, it creates a form like:

    form2 frm2 = new form2();

    Inside form2 there is a db connection. When the connection is not valid, it returns a messagebox and after that Application.Exit();

    But it doesnt exit. It continuous loading Form1. Does anyone know what I am doing wrong?

    Thankyou very much,

    Abadi

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Application.Exit() Doesn't close.

    Instead of using Application.Exit() you could show form2 as dialog. In this case, form1 will close the application when form2 'fails':

    Code:
    form1.cs:
    
          private void form1_Load(object sender, EventArgs e) {
             form2 frm2 = new form2();
             if (frm2.ShowDialog() == DialogResult.OK) {
                //do stuff
             }
             else {
                Close();
             }
          }
    
    
    form2.cs
         private void form2_Load(object sender, EventArgs e) {
              //try to setup DB connect
              bool succeed = StartDbConnection();
    
              if (succeed)
                 DialogResult = DialogResult.OK;
              else
                 DialogResult = DialogResult.Abort;
    
          }

  3. #3
    Join Date
    Jan 2009
    Posts
    2

    Re: Application.Exit() Doesn't close.

    Thank you very much,,

    it worked with a similair method,, When I use the showdialog, it shows the dialog what not should be. This is what I've done:


    Form2
    public boolean go;
    try
    {
    DBconnection;
    go = true;
    }
    catch (Exception er)
    {
    MessageBox.Show(er.Message, "Error");
    go = false;
    }

    Program.cs
    if(frm2.go == true)
    {
    Application.Run(new frm1());
    }
    else
    {
    Application.Exit();
    }

  4. #4
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Application.Exit() Doesn't close.

    It looks like that is the Main method. Why call Application.Exit() then?

    Code:
          [STAThread]
          static void Main() {
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
    
             if(frm2.go == true) {
                Application.Run(new frm1());
             }
             else {
                 //do nothing, application will not start
             } 
          }
    First, you can replace " if(frm2.go == true)" by "if(frm2.go)", because go is a boolean

    Second, please use code-tags when you post code. use [ code] your code here [/ code] (without the spaces)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center