简体   繁体   中英

Session Ending event not working

I want to let the user decides if he wants to do a last request at my app when the the Windows is shutting down or logging off. So I am using the "SessionEnding" event.

The following code is triggered but doesn't work. Here is the simplest example:

public App()
{
        this.SessionEnding += App_SessionEnding;
}

void App_SessionEnding(object sender, SessionEndingCancelEventArgs e)
{
        e.Cancel = true;
}

I am afraid that I've used a boost program before in my computer (like CCleaner), and somehow it deactivated this event. =O

When I add a MessageBox to the event, and I request to log off my computer, then the MessageBox appears, but I don't have time to click somewhere because after 1 second, the system log off. The system seems to not be waiting for my application.

Obs: I am using Windows 7.

I tried a sample of your code without success ... however I put this into my main window that causes the application to close and the MessageBox was displayed indefinitely until I clicked close. Could this help?

protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
    {
        MessageBox.Show("Blah", "Blah", MessageBoxButton.OK);

        base.OnClosing(e);
    }

Try overriding the event handler that's already there.

https://wpf.2000things.com/2011/01/25/197-override-application-class-methods-for-standard-events/

 public partial class App : Application { protected override void OnSessionEnding(SessionEndingCancelEventArgs e) { // Always call method in base class, so that the event gets raised. base.OnSessionEnding(e); // Place your own SessionEnding logic here } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM