Application is preventing Windows from shutting down - c#

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

Related

Windows form application not closing properly

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();

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.

C#.NET : A graphical control makes my app freeze after workstation unlock

I have a graphical control that is multithreaded.
Until now it worked fine, but I just noticed that whenever I'm on my application showing this control the following happens: if I lock and unlock my workstation, it freezes, like if it were in some kind of infinite loop.
Even stranger, this bug occurs only when I'm not launching the app from Visual Studio, and not attached to it.
Does anyone have a clue on what's happening?
For instance, if I attach Visual Studio to the already freezed app, can I see which lines of code my threads are executing?
Any help will be appreciated!
We recently had (for about a year and a half ;) this same problem. It also triggers sometimes when IE flushes caches, when you change colors of you theme. etc.
The problem was that we had a splash screen that had its window created on one thread and then it was shown (ShowDialog()) within other thread. Once we moved the window creation to the same thread that actually shows it, it resumed. There was also some changes with .Dispose():ng the splash window, and they could also have had an effect.
Microsoft has an article about this and they basically suggest to run their Spy++ program and look at your program when it's hung. There is a "Windows" -window, search for your application and look for any windows that should not be there. They possibly have a windows message pump active/attached but it is not pumping. The "change" message does not get handled and all .NET windows stall -> hang.
just attach VS to the frozen app and hit Pause button, VS will show executing code.

C# windows application not closing

I have a C# windows application. I placed it on a test server, whose set up is not controlled by my company and neither is the seurity context. I double click the exe. App runs and i see my form. I close the application, i open task manager and i still see a foot print of the applicatiion.
taskkill does not seem to remove it and it is still running in task manager.
how do i check if any resource is still being held?
The likely cause is that a background thread is still running after your application is closed. Depending on your framework and application configuration a background thread can cause a process to keep running even after the main window is closed.
Do you have any threads in your process? If so make sure to close them out when the main application window is closing. A good place to do this is in the OnClosing method of a Windows Form
Abusing Application.DoEvents() is another way to get into this kind of trouble. If you cannot kill the .exe from TaskMgr, your app is stuck waiting for a driver to finish an I/O request.

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