Tiernan Watson
Tiernan Watson

Reputation: 313

Exit application when all forms hidden

If you are building a windows forms application in C# and whenever the user opens a form from another form, the first form becomes hidden, how would you get the application quit if all forms were hidden?

I would have though of having instances of all the forms in a static class and have the application loop through the status of all the forms each time the user hides a form and if all are hidden then exit the application. Is this a good way to do it?

Upvotes: 0

Views: 185

Answers (1)

Scott Chamberlain
Scott Chamberlain

Reputation: 127563

Usually in this situation you would create a ApplicationContext and you would pass that in to Application.Run( instead of a specific form.

If you look at the MSDN page for ApplicationContext it has a example of starting two forms then only closing the program when both forms get closed. You can make your own program use whatever logic you want, for example using hiding instead closing to trigger the ExitThread() call.

Upvotes: 3

Related Questions