Exit application when all forms hidden - c#

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?

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.

Related

Windows form controlled by main loop

How can I control windows form class in the main loop?
Can I still run my code in the main loop like a console program but use windows form to display stuff? I already done that I've got windows form application and console in one but once main calls windows form that's it, it doesn't come back to main.
Can I pass variables to windows form?
You can't.
When you call Application.Run, the method only returns when there is no active Form any more. This is because Application.Run keeps the message loop running. It blocks the current method form returning. See it as a while (mainFormActive). It will never end unless the main form is closed.
If you want to execute code in the mean time, set up a Task, a Thread or similar, before you start the form.
And yes, you can pass variables in, just like in any constructor.

Having two active windows in a Winform application

Sorry, this more than likely has been asked at some point but I'm not even sure what I should be searching on...
I have a winform application with multiple forms. Up until this point having a one form open at a time has been fine. But now, I have a new form that I want to add on but have the ability to keep that form open while I work in other forms. I'm not even sure what this is called but I have seen it done before in other applications.
I did find this: Run two winform windows simultaneously
But this new window is a winpipe queue viewer that runs a thread. When I try initializing using the
Application.Run(new QueueViewer());
I get the error:Starting a second message loop on a single thread is not a valid operation. Use the Form.ShowDialog instead.
The problem with that is it locks the program from doing anything else until I close that form.
Thanks for your help!
Add a form to your project (let's call it Form2). Somewhere within your code (maybe in a button click event) use the following code:
Form2 f = new Form2();
f.Show();
The Show method allows you to interact with the originating form, whereas ShowDialog prevents interaction from the original form.

FContinueMessageLoop marks hidden modal dialog forms for closing

I am opening multiple modal dialog forms (2) one on top of the other. I hide the modal dialog forms along with their parent and open the Microsoft Word application. A short while after calling the Hide method for each form I noticed that the system calls the Close method automatically for each of the forms as a result of running FContinueMessageLoop (I noticed this in the StackTrace).
After reading this forum entry, I found out that this might be caused by cross-threading issues but the author is not very specific.
Does anyone know which are the situations in which FContinueMessageLoop decides that a message loop should be terminated?

Start a C# Application without a form, but that include forms

I am using MVC in my application, but with a small difference: 4 layers. It contains Model, View, Control and an Orchestrator. This is the main controller, and is the one who says which view controller is active at a time.
Therefore, I must have my entry point calling this Orchestrator class, which will create a controller and this controller will create the view, which will be shown. Whenever the views are closed, the application must stop and finish also.
However, I am not able using Application.Run inside the Main function, in Program.cs to run a class which is not a form. How is it possible to do implement the architecture I wish?
Sounds like you want the overload of Application.Run that takes an ApplicationContext.
The example on that page shows how to use ApplicationContext to exit the app when all of your forms are closed (rather than the default, of exiting when the main form is closed). You should be able to adapt this to your use.

windows mobile releasing resources when application is closed (.net 3.5 cf)

I have a basic .net 3.5 cf application with 4+ forms. I am using a window handler class that I created to make sure that certain forms only have one instance open at a time where as other (Product Details for example) can be opened as many times as the user wants. My problem lies in the fact that when the user closes all the forms (By clicking the "x" on the form rather than the "exit" button in the menu) that the application does not release the database connection. In addition to this if the user closes all the forms and then opens the app up again their previous search results are shown rather than a new form. How can I make sure to release all resources when the user closes all the forms??
The (X) button is a minimize button, not a Close button. You need to eitehr change the MinimizeButton on the Forms to false - which changes the (X) to an (ok) - or add logic to handle cases where all Forms get minimized.
Be aware that on Windows Mobile that clicking the 'X' is more like minimizing a window than closing it. It definitely won't exit the application, and may actually literally perform a minimize rather than a close on the form (I can't remember for sure)
So when they're "opening up the app again", it's likely it's just re-showing the same form.

Categories