【问题标题】:Session Ending event not working会话结束事件不起作用
【发布时间】:2013-10-05 10:43:37
【问题描述】:

我想让用户决定他是否想在 Windows 关闭或注销时对我的应用程序执行最后一个请求。所以我使用的是“SessionEnding”事件。

以下代码被触发但不起作用。这是最简单的例子:

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

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

我担心我以前在我的电脑上使用过一个增强程序(比如 CCleaner),不知何故它停用了这个事件。 =O

当我在事件中添加一个 MessageBox 并请求注销我的计算机时,会出现 MessageBox,但我没有时间点击某个地方,因为 1 秒后系统注销。系统似乎没有等待我的申请。

Obs:我使用的是 Windows 7。

【问题讨论】:

  • 这是您程序中的代码,还是您的实际代码中有 MessageBox?
  • 上面的代码是最简单的例子。但我也尝试在“e.Cancel = true”之前调用 MessageBox.Show()。它显示了,但系统一直关闭。
  • 试试你发布的代码,看看它是否有效。 Windows 不应等待您的 MessageBox 返回结果。我知道如果这样做我会很生气。
  • 我确实尝试了我发布的代码。它没有用=D。
  • 我正在与同样的问题作斗争。如果应用程序花费的时间太长并且异步部分有等待的东西,Windows 似乎只会杀死应用程序。

标签: c# .net wpf windows-7


【解决方案1】:

我尝试了您的代码示例但没有成功...但是我将它放入我的主窗口中,导致应用程序关闭并且 MessageBox 无限期地显示,直到我单击关闭。这有帮助吗?

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

        base.OnClosing(e);
    }

【讨论】:

  • 感谢您的尝试。我会考虑的。
  • 不,它不起作用 =/。我运行该应用程序并在 Windows 上注销,但它不起作用。系统不等什么就简单注销了。
  • 您是否有一个单独的主登录窗口。代码应该在关闭应用程序的窗口中
  • 你的意思是当我在 Windows 上单击“注销”时你让它工作了吗?
【解决方案2】:

尝试覆盖已经存在的事件处理程序。

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

【讨论】:

    猜你喜欢
    • 2013-05-10
    • 2012-03-20
    • 2012-10-15
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    相关资源
    最近更新 更多