Catch windows shutdown event in a wpf application - c#

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...

Related

Application.SessionEnding event not invoked when ACPI shutdown button pressed and display turned off

I've been developing a WPF application that targets the .NET Framework 4.5.1. It is running on Windows 10. The machine has an external ACPI shutdown button. When pressed Windows starts the shutdown process.
Part of the code attaches to the Application.SessionEnding event and when invoked does some processing. The first line of code in the attached delegate logs a line something similar to: "Received PowerOff" etc. The logline is always present when I press the shutdown button while the display is turned on and Windows starts the shutdown sequence. But if the display is off, when I press the shutdown button it only turns the display on and nothing else happens. The shutdown sequence is not invoked by Windows.
Did someone else experience this behavior? Is there any documentation or article about it? What are my options? I want Windows always to invoke shutdown sequence on button press. Not only when the display is turned on.

How can I listen to a System.exit() with an error in WPF?

I`m currently trying to handle cases in my app for exiting, such as Application.Exit (Both via .xml and in .xaml.cs), Dispatcher Exception, etc.
However, I can't seem to find any way to simply listen to a System.exit() command with an error. An example of this is when the app is running and it is stopped in Visual Studio (Shift+F5), where it logs that a System.exit(-1) happened.
Is there a way to listen to this in the Application context?
You can subscribe to the closing and/or closed event in your view.
If you subscribe to the events in your main view this may do the job for you.

Log4Net won't create custom event log in event viewer

My WPF app uses Log4Net to record messages to the event viewer. This is working great on most machines. However, there are two machines in my office where there are problems. One is a physical Windows 7 machine with 2 GB of ram, the other is a virtual machine running XP, which also has 2 GB of ram.
The problem is that even though the users are logged in using accounts with administrator rights, the system won't let them create the custom event log that I set up for my application. This is causing my program to die.
I can add error handling on all of the Log calls, but my feeling on this is I shouldn't. The messages are being logged in the catch handler for another error that already occurred. Just what am I going to do with the error information if it can't be logged?
In any event, I tried to create the custom event log on the XP virtual machine yesterday and it still wasn't created. What exactly do I need to do to get the custom event log created on these two machines?
Tony
It turns out that the problem wasn't in the logging code at all. My program uses WPF for the GUI. It's start-up sequence does the minimum amount of work on the UI Thread so it can display the UI as soon as possible.
The rest of the initialization is done on a background thread. I knew that an error was occurring, but I couldn't find the custom error log in the list of logs in the Event Viewer. It turns out that my code didn't find some data in the database that it needs and was trying to report the error. This is a 2 step process which involves first recording the error to the log and then displaying a custom MessageBox dialog. I was getting a XamlParseException when the program was trying to display this dialog.
To make a long story short, the problem that was crashing the program was the XamlParseException. This was thrown because I was calling the custom MessageBox's Show method on the background thread, not on the UI thread. Because I couldn't find the custom event source in the event viewer, I couldn't find the error, so I assumed that the error was a permisions issue.
By the way, I did try to create the event log manually at one point, and yesterday I checked the registry and did find the entry for the custom event source.
There is one other machine here that is having the same problem. I'm sure it's the same exact issue. I'm adding logic to the error handling to make sure that the custom MessageBox is always called on the UI Thread so the program won't bomb like that if the same issue recurs.
We would need to see how you tried creating the event log on the XP machine...
Generally, you need to read this: http://msdn.microsoft.com/en-us/library/49dwckkz(v=vs.80).aspx
Particularly the note discussing when to create your custom event log:
"In general, create the new event source during the installation of your application. This allows time for the operating system to refresh its list of registered event sources and their configuration. If the operating system has not refreshed its list of event sources and you attempt to write an event with the new source, the write operation will fail. If creating the source during installation is not an option, then try to create the source well ahead of the first write operation, perhaps during your application initialization. If you choose this approach, be sure your initialization code is running with administrator rights on the computer. These rights are required for creating new event sources."
Try creating the custom log in advance of the first logging event to use it.

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...

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.

Categories