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

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.

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

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

What event to use in WPF to update Application after Computer was Resumed after Hibernate or Sleeping?

I have WPF App, that needs to be update some data , after computer was resumed from Hibernate, or Sleep mode. Details.
If I keep Application open, and sleep computer (or hibernate) after I resume computer, I want to see Application Data updated. Without any click , or focusing..
I tried to use Application.Activated event, but it only works when I click or activate Applicaiton.
What Event Should I use?
Thanks a lot.
You can have a look at the SystemEvent class. Probably PowerModeChanged will work.
I think you should do this with the Windows APIs directl, overriding WndProc and intercepting messages:
check this one: Can't catch sleep/suspend messages (winXP)
for example on resume from hibernation you would get WM_POWERBROADCAST message with PBT_APMRESUMEAUTOMATIC flag/parameter...

Catch windows shutdown event in a wpf application

I have a c# WPF application that needs to save data when it closes. The Window Closing/Closed events work fine if the user closes the program, but they do not get called if the user logs off/shutdown the computer.
I have found ways to catch this event in winforms programs (here, and here). but i cant figure out how to achieve this in a WPF application.
I'm trying to halt the shutdown until my program is ready to exit
There is a built-in event Application.SessionEnding - this event fires when the user logs off or shuts down the computer... you just need to subscribe to that and put your code to save date etc. in there...

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.

Categories