My WPF app still running if I open another Window - c#

I'm writting an WPF application using the mvvm toolkint.
In the main windows I have a command in a button that open another window using:
catView.ShowDialog();
Well, I close that window (using a close button or the X) and when I close the main window, the app is still running and I have to kill it.
If I don't open the second window, the app shutdown normally.
Why if I open another window I can't close the app normally?
I have this in the close button of the second window:
this.DialogResult = true;
this.Close();
On the other hand, I start the app in this way (mvvm toolkit way):
Views.MainView view = new Views.MainView();
view.DataContext = new ViewModels.MainViewModel();
view.Show();
Thank you very much.

The problem is probably unrelated to opening and closing the window but is somthing inside that window.
This usually happens when you have another thread still running when you close the application, check for anything that might be creating a new thread inside the window's code (including System.Threading.Thread, ThreadPool, BackgroundWorker and 3rd party components), make sure all background threads shut down before closing the application (or if you can't shut them down at least mark them as background threads).
Also look for anything that can open another (invisible) window, it's common to use window messages to an invisible window as an inter-process communication mechanism, again look for 3rd party code that might be doing that.

Nir is correct, a thread is probably still running in your other window.
You can fix this by terminating the application's thread dispatcher when the window closes.
public Window1()
{
InitializeComponent();
// This line should fix it:
this.Closed += (sender, e) => this.Dispatcher.InvokeShutdown();
}
I'm happy to be corrected if this is not the right way to do things. Worked well for me though.
PS.
If your other window is designed to run in a different thread, then read this by Eugene Prystupa:
running-wpf-application-with-multiple-ui-threads

I don't know if this is causing your issue or not, but you don't need the call to Close() in your second window. Setting the DialogResult automatically closes the window.

Related

How to close the app when I make click on X button in a child form?

I have a form app with two Forms. In the second form I have in the right corner the x button. How I can make when I make click on this button to close the app, not just hide the Form2 window?
First you need to catch the event. To do that, set an event handler on the child form's FormClosing event.
Then there are several options:
"Brute force" termination using Process.Kill().
This will terminate the process without letting any cleanup code to run. It has an effect like ending a process through the task manager. You can get the current process with Process.GetCurrentProcess. Use like this:
Process.GetCurrentProcess().Kill();
"Gentle" termination by way of closing all windows using Application.Exit().
This will close all message pumps and windows, but will do so while allowing normal cleanup code to run. It does not however guarantee the process will be terminated, for example if a forgound thread is still active after message loops are done. Use like this:
Application.Exit();
Communicate intentions to the main thread.
This is a design solution, not a "line of code" you put somewhere. The idea is that the 2 classes (of the 2 forms) have some communication mechanism (via message, events or whatever you see fit and probably already use), and the child form notifies the parent form the user wants the exit the application. Then it's up to the main form to cleanup everything, close all forms (itself and others), and exit the process normally. This is the cleanest and preferred method, but requires a proper design and more code.
If you are using form the simplest is using
this.Close(); or Application.Exit();

Process does not kill on form close

I have just built version one of my testing application using Windows Forms. I have noticed that when running the application, it runs completely fine no hitches, exactly like the debug view. When it comes to closing the application I have noticed that the actual executable/process name hangs within Task manager and does not correctly close.
Upon further inspection I have noticed that when calling another form without hiding the previous form, a new process is spawned (kinda expected). When closing the new form (containing a few text boxes, labels and a DataGridView) the newly spawned process does not kill it's self, but remains. Then closing the main window the window disappears from the taskbar/view, but still, the processes remain active using 8,268k - 8,308k Memory
private void ClientSearch_Click(object sender, EventArgs e)
{
ClientSearch Clientsearch = new ClientSearch();
Clientsearch.Show();
}
Standard explanations for this behavior:
Hiding your main window when you display another window and forgetting to unhide it. There is no visible window anymore, nor can the user do anything to unhide it, but your app keeps motoring of course.
Starting a thread and not making sure that it is terminated when the main window closes. Setting the thread's IsBackground property to true is a workaround for that.
Calling Application.DoEvents() in your code. A very dangerous method that permits you to close the user interface but doesn't stop the loop in which it was called so the main thread of your app does not exit either.
This kind of problem is readily visible as well when you debug your app. You might have gotten in the habit of using the red rectangle on the toolbar (aka Debug + Stop Debugging) to force the debugger to quit. The Debug + Windows + Threads debugger window can provide insight into the cause of the last two bullets. Or you can use Tools + Attach to Process to attach the debugger to a zombie process.
Call
Application.Exit();
on form close/closing.
Your applications should only be creating one process per run. A new form should not be creating a new process.

Intercept Opening Window - C#

Is there some way to watch for / intercept a window opening in Windows and then to block said window from opening? This would be for an application that's already running and I wouldn't want to stop the process, just close the popup window it spawns.
Update:
A process is already running (e.g. it's in Task Manager) but has no visible windows.
At some random time, that process will popup a window
I manually close the window (click "OK").
The process continues to run at this point and will, again, popup a window a bit later (repeating these steps).
I want to automate step #3, where I have to manually close the popup, by intercepting that window opening and closing it or hiding it or never letting it open in the first place. I don't want to do this by polling the open windows. I want to receive some event that a window is about to open.
I do not control this other application, so I can't otherwise change it. And I don't want to kill the process, itself.
If you're trying to do what I asked (allow only one instance of a program to run at a time), here is an elegant solution using a Mutex. You can probably copy and paste most of that code to achieve what you want.

How to dispose a WPF application GUI?

I have a WPF application with a GUI on the MainWindow. I want to dispose this window after a while based on some user actions. But I want the application continue to run in background. I know that Window can be set to hidden, but it still uses the memory.How to destroy the MainWindow without quitting the WPF application? Also I would like to know if it is possible to relaunch the MainWindow after it was disposed.
this.Close(); //Will close the window but keep the application running.
var mw = new MainWindow();
mw.Show(); //Will open a new MainWindow and show it.
mw.Close(); //Close this one too.
That said hiding it is the best choice, the amount of memory it uses should really be irrelevant. That's what memory is there for.

Can we and how to open a new window and closes the previous window in the same thread?

I am asking this, because i want to know if when we are running an app, for start if we have an window to authenticate like a Log In window, after validating the user, can we open the Main Window in the same Thread without creating a new one?
I am trying to do this in WPF, but i think that is same thing in WPF or in Windows Forms.
Yes, you can.
Just do it.
When you generate a Windows Forms application via the IDE, it will generate the code for one form, as well as a Main function that displays the form at runtime. You can rewrite the Main method so it displays one form modally then displays the next form.
But there's a simpler way to achieve your objectives:
Have two windows: your Main window, where most of the work is done, and the login screen.
In the OnLoad event of your main window, create an instance of your login window and call ShowModal() on this instance.
If the login fails, then exit the application.
This question does not offer enough context to tell you how to do this in your specific case. In general you can just Close() a window, construct a new one and call Show() on it.
You should make sure the Application.ShutdownMode does not kill off your application when the window is closed though.

Categories

Resources