I have tried around with threads a little bit but I can't figure out the right way to get what I want (because I'm not very familiar with threads).
I have a "Sign out" button in my main form. When clicked then the Form shuts completely down and restarts (Application.Restart()). Now I wanted another form pop up when the main form gets closed (Just a label with "Loging out - Please wait"). When the main form opens then this "Log out form" should disappear.
I know I should make the Sign out form as foreground thread but how do I call the Form.Open() method with the thread and how I can get it closed when the main form starts?
Try to show the second form in the FormClosing event of the main form.
When the main form closes it most certainly "shuts down" the message loop of the application making it impossible to spawn any new window or closing any window that is already open.
Edit:
As of the comments: Showing the logout form from the process that is about to Restart until the new process has been started successfully will not be possible because the old process will be shut down and therefore any open form it shows.
Possible solutions:
Write a program with the solely purpose of showing the logout form and start that before restarting the application. After the Application has startet again search the process of the logout form and stop/kill it.
Avoid to have to restart your application completely
Related
I've ran into a weird bug where a WPF app is closing after the first window closes. This has made me curious what is triggering it to close
For example this is what I was the unworking version. After the EULAWindow would close it would shut down the application.
EULAWindow eula = new EULAWindow();
eula.ShowDialog();
MainMenuWindow mainWindow = new MainMenuWindow();
mainWindow.ShowDialog();
I thought it would open the EULA and then open the Menu window. I've found that if I rearranged the code it will operate like expected.
For example this will fix it:
EULAWindow eula = new EULAWindow();
MainMenuWindow mainWindow = new MainMenuWindow();
eula.ShowDialog();
mainWindow.ShowDialog();
This brings me to the question of what actually is causing the application to shut down in the first situation?
ShowDialog() is a blocking call. So, in the first case you have single window, then show it, then let user to close it. What should an app to do once last UI window is closed? Maybe, follow to app shutdown code? I suspect so.
In the 2nd case you create two windows before the blocking inside ShowDialog(). So, once you closed the first window, there is another one that could accommodate the user, and the app is not shutting down, it just waits for the next window to be shown.
Since I was using Application Startup to create my windows, it was defaulting setting the EULAWindow to the Current.MainWindow Then keeping the Current.ShutDownMode as ShutdownMode.OnMainWindowClose.
When you create two windows, before calling Show or ShowDialog, it changes the shutdown mode automatically to ShutdownMode.OnLastWindowClose
I'm not certain how or where this happening, but I can tell this is the behavior that is happening upon further investigation.
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.
Why is it that if I call a WPF form from another project type (e.g. a console application or XNA game), the main application doesn't wait for the form to close before ending (and subsequently closing the form)?
I know with a dialog box I can make the main class wait for a reponse, how can I make it do that with my form?
You can open the window in modal mode using the ShowDialog method - the ShowDialog method only returns after the window was closed. Otherwise you can either wait until the Closed event is fired or wait until Application.Windows collection is empty (meaning the application has no WPF windows left).
For more information about window closing in WPF, refer to this.
in my application there is a print form. When you click the print button, the windows print dialog appears, showing the printed page.
When it's finished the print form is closed (this is still ok) but my application is set to the background on the z-order (this is not ok) and another application window is set to the foreground.
I could help myself by calling WinAPI-SetForegroundWindow() from my application, but it flickers and I don't think that this is a clean approach.
A clean approach would be that my application doesn't get set to the background.
Any suggestions?
try to call .ShowDialog with your form as the first parameter. that should bring your form into the front after the dialog is closed.
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.