I modified an app the way I wanted. But seems like there is a small problem.
When I close it, the process associated with the app stays open and it's not terminating.
Yes, the RAM that it consumes decreases, but seems like something is not disposed of and this keeps the process running. How I can find what prevents the process from closing?
Its a program, written in C# WPF, which I modified by adding more windows, cycling between the windows, which open each other with
Form frm1=New Form()
this.Hide()
The second I close directly with
this.Close()
While before this I call other form.
The most simple way is to make it call batch that terminates the process on window closing. But I want to find out what is letting the process to terminate.
There Seems an instance is still running ( Not disposed the right way ) ...
Try to check if frm1 and other Instanciated Forms are disposed well , else dispose them on the on the main Form's Exit event .
if (!frm1.IsDisposed) frm1.Dispose();
do same to other instances .
Related
Following are the ways by which we can exit an application:
Environment.Exit(0)
Application.Exit()
Form.Close()
What is the difference between these three methods and when to use each one?
The proper method would be Application.Exit(). According to the Documentation, it terminates all message loops and closes all windows thus giving your forms the possibility to execute their cleanup code (in Form.OnClose etc).
Environment.Exit would just kill the process. If some form has e.g. unsaved changes it would not have any chances to ask the user if he wants to save them. Also resources (database connections etc.) could not be released properly, files might not be flushed etc.
Form.Close just does what it says: it closes a form. If you have other forms opened (perhaps not now but in some future version of your application), the application will not terminate.
Keep in mind that if you use multithreading, Application.Exit() will not terminate your threads (and thus the application will keep working in the background, even if the GUI is terminated). Therefore you must take measures to kill your threads, either in the main function (i.e. Program.Main()) or when in the OnClose event of your main form.
they are all fine.
but form.Close() won't close your application
it closes the form and after that
the main-method returns an int (exitcode).
if you want that your application exits with exitcodes use
Environmet.Exit(exitcode) or return the exitcode in the main-method
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();
I have a simple WinForms application that runs in the system tray. Is it possible to password protect the program from closing if a user tries to close it from task manager?
Ideally I want to keep a user from closing the process but if windows is restarted, I want it to close without being prompted. Much like antivirus programs.
If a user has the correct permissions they will be able to kill your process from the Task Manager.
That said as #fujiFX mentions in his comment the FormClosing event is a good start and better than nothing.
The FormClosing event occurs as the form is being closed. When a form is closed, it is disposed, releasing all resources associated with the form. If you cancel this event, the form remains opened. To cancel the closure of a form, set the Cancel property of the FormClosingEventArgs passed to your event handler to true.
It's hard to do, but you can create two applications, one is main program and two is helper. If helper is killed, then main app restarts it. If main app is killed, then helper restarts main app.
You can make this verification on YourService.Stop method (assuming you're using a Windows Service thus inheriting from ServiceBase), but as far as I know you cannot prevent the user from killing the proccess, just to close the app (two different things in Windows).
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.
From here Does closing the application stops all active BackgroundWorkers? it seems not.
But from here How to stop BackgroundWorker on Form's Closing event? it seems yes.
So which is it?
(EDIT: I realize that the BackgroundWorkers might exit with an exception. But what's the problem with that? Isn't the point here to not leave running threads which take up resources?)
Closing a Form does not stop all background workers started by that form.
When the entire application ends it will stop all background threads.
Closing the main form (unless you have modified the Main method to do something else) will end the entire application.
Each question you referenced is correct for what it says. If you close the main form, then the entire application will end and the background worker will be closed on its own. If the form that is closing isn't the main form, but some other form, and you want the background worker that it starts to be stopped, then you will need to do so yourself.
It's also worth noting that the second link that you have provided asks for something a bit more complex. It's clear in that post that closing the form (if it's the main form) will stop execution of the background thread. What the OP is trying to do there is to tell the background thread, "hey, it's time to finish up, we're done here" and then have the form wait until that background thread can finish cleaning things up nicely, rather than just exiting and forcibly aborting the thread while it's in the middle of doing something.
Both of those links that you provide have the correct answer- BackgroundWorkers will be closed when the program is closed. Unmanaged resources are the ones you have to worry about explicitly closing.