簡體   English   中英

系統重啟時未調用 Application.Exit 事件

[英]Application.Exit Event not invoked on system reboot

我有一個 WPF 應用程序,每次應用程序退出時都需要保存其當前狀態。 目前,我正在處理System.Windows.Application.Exit事件以保存應用程序的狀態。 但是,似乎該事件沒有被調用系統重新啟動——只是普通的關機。 這是預期的行為嗎?

這就是我的應用程序的樣子。

public class MyApp : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        ...
        this.Exit += OnExit;
    }
    private void OnExit(object sender, ExitEventArgs e)
    {
        SaveMyApplicationState();
    }
}

我可以使用哪些事件來通知系統重新啟動,以便可以保存我的應用程序狀態?

要確定系統何時重新啟動或關閉,請使用SessionEnding事件:

應用程序.xaml

<Application ...
     SessionEnding="App_SessionEnding">
    ...
</Application>

應用程序.xaml.cs

public partial class App : Application
{
    void App_SessionEnding(object sender, SessionEndingCancelEventArgs e)
    {
        if (.../*canceling*/)
        {
            e.Cancel = true;
        }
        else
        { 
            // Processing before terminating (save current state)
            // ...               
        }
    }
}

有關詳細信息,請參閱文檔: Application.SessionEnding 事件

注意: Windows Vista 中的應用程序關閉更改

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM