在关机前等到保存完成

时间:2018-02-19 20:15:54

标签: c# winforms systemevent sessionend

我有一个记录一些用户数据的应用程序。如果用户注销工作站,则需要保存此信息。

该程序是一个Windows窗体c#应用程序。

目前我正尝试通过以下功能执行此操作,它可以达到95%,但不会达到100%。

    Application.Run(new Shutdown(_object));

    private void Shutdown_FormClosing(object sender, FormClosingEventArgs e)
    {
        e.Cancel = true;``
        _object.LogLogout(); //Save the logout to a file

    }

我也试过了这个功能,但这根本没用。我不明白为什么。任何提示?我完全被困在这里。

    Microsoft.Win32.SystemEvents.SessionEnded += new Microsoft.Win32.SessionEndedEventHandler(SystemEvents_SessionEnded);
    Microsoft.Win32.SystemEvents.SessionEnding += new Microsoft.Win32.SessionEnding(SystemEvents_SessionEnding);

1 个答案:

答案 0 :(得分:1)

找到答案只有表格仍然可见时才会停止关闭。事实并非如此。因此我通过这样做解决了这个问题。

    private void Shutdown_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (systemShutdown)
        {
            this.Show();
            //If GUI got closed delted active entry and log off
            _object.LogLogout(); //Save the logout to a file
            Thread.Sleep(100);
            this.Hide();
        }
    }
相关问题