Exiting from an app

P

pnp

Hi all,
I'm working on a win app that uses a logon form to verify the user that
logs in the program and then hides the first form and displays an MDI
form where the user does his work. The app is STA.
The problem is that I wan't to be able to close the app from wherever in
my app (no matter how deep in the code) and make sure that all the
resources are being freed after exiting the app. Any ideas?
Is there a way to re-open the app programmaticaly after it has been
closed so that another user can login?

Thanks in advance,
Peter.
 
N

Nicholas Paldino [.NET/C# MVP]

Peter,

You could always call the static Exit method on the Application class.
This should send a shutdown message to your app, which would then close.

However, to get the kind of behavior that you want, I think that you
need to go through a loop, like so:



// In main section.
// Continue?
bool cont = true;

while (cont)
{
// Create login form and show it.
LoginForm loginForm = new LoginForm();

// Show in a message loop.
Application.Run(loginForm);

// Check a property to see if the form actually was logged into
correctly, or not cancelled.
// Make sure to name this property something else.
cont = loginForm.ShouldContinue;

// If continuing, then show the main form.
if (cont)
{
// Show the MDI form.
ClientForm clientForm = new ClientForm();

// Show.
Application.Run(clientForm);
}
}

This should give you the behavior that you want, and calling
Application.Exit anywhere from within your main client code should cause you
to exit (although I don't think that is a good idea, personally).

Hope this helps.
 
P

pnp

Nicholas said:
Peter,

You could always call the static Exit method on the Application class.
This should send a shutdown message to your app, which would then close.

However, to get the kind of behavior that you want, I think that you
need to go through a loop, like so:



// In main section.
// Continue?
bool cont = true;

while (cont)
{
// Create login form and show it.
LoginForm loginForm = new LoginForm();

// Show in a message loop.
Application.Run(loginForm);

// Check a property to see if the form actually was logged into
correctly, or not cancelled.
// Make sure to name this property something else.
cont = loginForm.ShouldContinue;

// If continuing, then show the main form.
if (cont)
{
// Show the MDI form.
ClientForm clientForm = new ClientForm();

// Show.
Application.Run(clientForm);
}
}

This should give you the behavior that you want, and calling
Application.Exit anywhere from within your main client code should cause you
to exit (although I don't think that is a good idea, personally).

Hope this helps.

Thank you very much for your answer. It was a big help to me!
Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top