How to end process after closing whole application in C#.NET?
Asked Answered
B

7

2

I have a .NET 2005 (C#) desktop application, in which there is a login form and an MDI form, which then have multiple subforms. All other forms are opened in MDI form only.

After a user logs in, I hide the login form and then show the MDI form, but when I close the MDI form, my application process does not end because the login form is still hidden. I want that, when the user closes the MDI form, the whole application should close (essentially, the process should not be shown in the task manager), because if everytime the user closes and reopens the application and logs in, it will create some performance problem.

I am doing something like below:

//my login validation script,

//after successful login

this.Hide();

if (globalData.ObjMdiMain.IsDisposed)
{
    globalData.ObjMdiMain = new mdiMain();
}
globalData.ObjMdiMain.Show();

globalData is my static class where I create global objects which are required by the whole application. There, I have defined the ObjMdiMain object of my MDI form and I am accessing it here in the login form.

So, is there any method or function which will end the whole process from system, something like "Application.End();" or something else?

Thanks!

Bouse answered 26/10, 2009 at 8:42 Comment(0)
B
0

It seems a MySql .net connector bug, because I was using mysql .net connector DLL to connect to my mysql db. I have seen some other complains about it too. See http://bugs.mysql.com/bug.php?id=36688

I was using MySQL .net connector DLL of version 5.1, I have just upgraded to 6.1 (latest) and its working fine without any error.

Download the latest DLL here: http://dev.mysql.com/downloads/connector/net/6.1.html

Thanks

Bouse answered 29/10, 2009 at 16:28 Comment(0)
O
8

I assume from your description (please correct me if I'm wrong), that you have the login form set as your startup form, like this:

    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new LoginForm());
    }

Don't do that. Change it to something like this instead:

    static void Main() {

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        LoginForm frm = new LoginForm();
        if(frm.ShowDialog() == DialogResult.Cancel) {
            return;
        }

        Application.Run(new MdiForm());
    }

In your login form code, do not call the MDI form at all, just validate the username and password and allow the login form to exit. The Main method will then call the MDI form properly.

Your application will then exit naturally when you close the MDI form.

Ozell answered 26/10, 2009 at 9:31 Comment(7)
Yes, you are correct I have the login form as Start-up form, let me try out your code.Bouse
Hi @Christian, I have tested your code but I am getting this farm3.static.flickr.com/2480/4052283130_893d670f92_o.png error, when I am closing my login form with this.Close() after successful login check.Bouse
I am using Windows7 with VS2005.Bouse
Sorry Prashant, I can't access that URL. What is the error message text?Ozell
Christian: The image url is working properly, anyways; I am getting "System.Threading.SemaphoreFullException was unhandled" exception with message "Adding the given count to the semaphore would cause it to exceed its maximum count."Bouse
Prashant, could you post your Main method code and your login form code, and I'll see if I can see the problem.Ozell
Hey @Christian, thanks but my problem got resolved. Your code was right and perfect. Problem was at my end. I am connecting .NET app to MySQL db for which I am using MySQL .net connector: dev.mysql.com/downloads/connector/net/6.1.html but I was sing 5.1 version of this DLL whereas its now reached to 6.1 So It was problem with the MYSQL connector DLL, I have upgraded to new version and its working perfect. Thanks for your help and support.Bouse
S
1

I find it strange that you leave the login window hidden after the initial login. Why not close it? Then you would not have your current problem: after successful login you only have the main MDI window that when is closed exits the running Windows Forms application. Is there a reason you hide the login window instead of closing it?

Stakhanovism answered 26/10, 2009 at 8:46 Comment(2)
Because Login is the main form of the application, when I am closing this then its ending the whole application.Bouse
I concur with Christian Hayter: don't use the default project structure of simply running a default form, but write your own startup code. Also see #406885 for an example that takes top level fatal exception handling into account. Your login GUI logic would go in place of the // whatever you need/want here in my example.Stakhanovism
B
1

I presume you've looked at Application.Exit?

Blub answered 26/10, 2009 at 8:48 Comment(0)
L
0

There is Application.Exit() which will send a windows message to close all forms and exit the message loop.

Luminous answered 26/10, 2009 at 8:49 Comment(4)
But its not doing it actually, its causing some unhanded exception.Bouse
So what exception is it causing?Blub
Its causing this exception: flickr.com/photos/prashantvictory/4052283130/sizes/oBouse
You have a thread safety problem elsewhere in your code. Find the root of the problem. Analyse the semaphore which threw the exception and review your code to find the problem.Luminous
D
0

I hate to break it to you, but... Application.Exit() is what you're asking for. Had you actually tried, you would have found it. Though, I have to agree with peSHIr, why are you keeping the login form around if it's causing you issues?

Digamy answered 26/10, 2009 at 8:49 Comment(0)
P
0

Here is a maner of closing the actual process, but it is the bad way to close an application :

Environment.Exit(Code) 

you can set the code at 0. or other integer, this will tell windows wath is the result of execution of the application.

I advise you to close the login form first and then shut down your application, this is the proper way.

Good luck ;)

Pyrotechnic answered 26/10, 2009 at 9:21 Comment(0)
B
0

It seems a MySql .net connector bug, because I was using mysql .net connector DLL to connect to my mysql db. I have seen some other complains about it too. See http://bugs.mysql.com/bug.php?id=36688

I was using MySQL .net connector DLL of version 5.1, I have just upgraded to 6.1 (latest) and its working fine without any error.

Download the latest DLL here: http://dev.mysql.com/downloads/connector/net/6.1.html

Thanks

Bouse answered 29/10, 2009 at 16:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.