Windows form application not closing properly - c#

I'm running a simple .NET Windows Forms application. When I click the close button, the Windows form gets closed, but the process is not closing. When I look at the Task Manager, I see that the process for the application is still live, as a background process.
Can somebody explain why that is? Am I missing some function when it's closing?

You need to call
Application.Exit();
in your Form's closing event.

Application.Exit
Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed. This is the code to use if you are have called Application.Run (WinForms applications), this method stops all running message loops on all threads and closes all windows of the application.
// WinForms app
System.Windows.Forms.Application.Exit();
Put this code on you from's closing event.

The main reason for not close properly, When your application works with the multi threading. You should use the following, it's works for me as well.
Application.ExitThread();

Related

Application is preventing Windows from shutting down

I have a Windows Forms application which starts a couple of background workers and has a main UI thread running a form.
This application sometimes prevents the user from logging off, restarting or shutting down.
It shows a screen like the following:
The only solution I got is subscribing to the FormClosing event of my mainform and force the closing of the program like shown in the following answer.
Following this solution I was able to terminate my program without waiting indefinitely on the screen shown above but I still get it for few seconds.
How can I prevent the windows from showing
This program is preventing Windows from (restarting/shutting down/logging off)?
I tried other answers on similar question:
Made sure all my other threads are background threads
Subscribed to the Form_Closing event (Partial solution, windows closes the application eventually but the message appears for a few seconds)
Made sure no other forms are implementing Form_Closing and holding the application

shutdown wpf while messagebox open

I try to automatically shut down an wpf application at midnight with:
Dispatcher.BeginInvokeShutdown(DispatcherPriority.Normal);
This works very well in general, but when there is a message box opening waiting for user response, the application fails to shutdown. Is there a way to shut down the application regardless of the opening messagebox?
For WPF Applications use
Application.Current.Shutdown();
Maybe you can use Environment.Exit (immediately exits...very naughty to do on a GUI app) or find the MessageBox window and send them a close message, or hook the creation of any native MessageBox Dialogs (...i.e. track the Window handle, so you can then close them).
Close C# Console Application after MessageBox click
Force to close MessageBox programmatically
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/d3f89686-e4d0-4bb1-9052-31abef2a9d2a/
Closing Applications
Right way to close WPF GUI application: GetCurrentProcess().Kill(), Environment.Exit(0) or this.Shutdown()
http://msdn.microsoft.com/en-us/magazine/cc188920.aspx
And a very very naughty way:
Process.GetCurrentProcess().Kill()

c# windows can't close the app in the shutdown

I have this problem:
I built an application in c# .net 2.0 that is on the tray bar and everything works fine: if I click the close menu (that i've added) I call Close(); of the main form and everything is ok.
my problem is: if a person shutdown the pc without closing my application, windows seems to be not able to close this program and the shutdown routine is breaked.
a note: in my app I use a BackgroundWorker.
thanks in advance
If your application is doing something that is stopping windows from shutting down properly, you should handle the SystemEvents.SessionEnding event in your application. This event is fired when the system is shutting down or the user is logging off.
In your event handler, do whatever is necessary to allow your application to be terminated gracefully, such as stopping all background workers / threads - etc.

Windows Mobile App will not run more than once

I'm having problems getting an application I wrote for Windows Mobile 6.0 to run more than one time on my smart phone.
I built it into a .cab and it installs and runs fine the first time, but if I close the application and try to start it again, it will not run unless I restart the phone.
I have checked the task manager after closing the application and it does not show up.
Are you sure that you are closing your application properly? I recommend using a process manager to see if your application still runs on the background.
My problem was in not handling closing the main Form when the application exited from one of the other forms. Ex: application starts with calculator loaded, user loads unit conversion form from calculator form, and the app hides the calculator. User then closes the unit form and calculator is still running.
All that I missed was handling an OnClosed event in my main form for when the user tried closing the application from one of the other forms.

Why does my application not close on logoff/shutdown (c#/.net winforms)?

My winforms app isn't shutting down nicely when I log off/shutdown. I have a main form, whose Closing event is fired correctly, but there must be something else keeping my application around. If I check Application.OpenForms there's just my one main form.
The tricky bit, and where the problem probably lies, is that my application uses ShellWindows to hook into Internet Explorer, and occassionally opens up forms when IE events fire. It's after one or more of these forms has been opened and closed that my app stops closing on shutdown.
I think I'm cleaning up all form objects etc and calling FinalReleaseComObject() appropriately, but I guess there are references somewhere that are holding my process open. Is there any way to work out what it is that's stopping my app from closing gracefully?
An application will also stay open if there are threads running that have not been set to background. If you are creating any of your own threads, make sure that they are terminating appropriately.
If it is not critical that the thread finishes, set IsBackground to true. You can also call Abort on a thread to (somewhat)forcibly kill it.
The most likely cause is that you have a background thread hanging around that is not being closed when the main window of your application is closed. Depending on your settings and framework version background threads can keep an application alive when the main thread is terminated.
During shutdown windows asks every running app to terminate usually by sending a WM_QUIT to the main window on a process. WinForms will happily use this message to shutdown the main window but if any background threads are left the actual process could continue.
This is a really ugly way to do this, but if all you want to do is kill any thread that's hanging around, you can get all the threads running in your application with System.Diagnostics.Process.GetCurrentProcess.Threads, and then enumerate through them and call Thread.Join() or Thread.Abort() on them.
Just make sure to NOT call .Abort() on the main (UI) thread that you're working from (the one that receives the Closing event). So make sure to check that your current thread (System.Threading.Thread) is not the one you're aborting.

Categories