简体   繁体   English

会话结束事件无效

[英]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. 我想让用户在Windows关闭或注销时决定是否要在我的应用上做最后一次请求。 So I am using the "SessionEnding" event. 所以我正在使用“SessionEnding”事件。

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. 我担心我之前在计算机中使用了一个增强程序(比如CCleaner),并且它以某种方式停用了这个事件。 =O = 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. 当我向事件添加MessageBox,并且我请求注销我的计算机时,会出现MessageBox,但我没有时间点击某处,因为1秒后系统会注销。 The system seems to not be waiting for my application. 系统似乎没有等待我的申请。

Obs: I am using Windows 7. Obs:我使用的是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. 我尝试了你的代码示例但没有成功...但是我将它放入我的主窗口,导致应用程序关闭,MessageBox无限期地显示,直到我点击关闭。 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/ 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 } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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