處理應用程式暫停

重要 API

了解如何在系統暫停您的應用程式時,儲存重要的應用程式資料。 此範例會註冊 Suspending 事件的事件處理常式,並將字串儲存至檔案。

註冊暫停事件處理常式

註冊以處理 Suspending 事件,這表示您的應用程式應該在系統暫停之前儲存其應用程式資料。

using System;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;

partial class MainPage
{
   public MainPage()
   {
      InitializeComponent();
      Application.Current.Suspending += new SuspendingEventHandler(App_Suspending);
   }
}
Public NotInheritable Class MainPage

   Public Sub New()
      InitializeComponent()
      AddHandler Application.Current.Suspending, AddressOf App_Suspending
   End Sub
   
End Class
MainPage::MainPage()
{
    InitializeComponent();
    Windows::UI::Xaml::Application::Current().Suspending({ this, &MainPage::App_Suspending });
}
using namespace Windows::ApplicationModel;
using namespace Windows::ApplicationModel::Activation;
using namespace Windows::Foundation;
using namespace Windows::UI::Xaml;
using namespace AppName;

MainPage::MainPage()
{
   InitializeComponent();
   Application::Current->Suspending +=
       ref new SuspendingEventHandler(this, &MainPage::App_Suspending);
}

暫停之前儲存應用程式資料

當您的應用程式處理 Suspending 事件時,它有機會將其重要的應用程式資料儲存在處理常式函式中。 應用程式應該使用 LocalSettings 儲存體 API 同步儲存簡單的應用程式資料。

partial class MainPage
{
    async void App_Suspending(
        Object sender,
        Windows.ApplicationModel.SuspendingEventArgs e)
    {
        // TODO: This is the time to save app data in case the process is terminated.
    }
}
Public NonInheritable Class MainPage

    Private Sub App_Suspending(
        sender As Object,
        e As Windows.ApplicationModel.SuspendingEventArgs) Handles OnSuspendEvent.Suspending

        ' TODO: This is the time to save app data in case the process is terminated.
    End Sub

End Class
void MainPage::App_Suspending(
    Windows::Foundation::IInspectable const& /* sender */,
    Windows::ApplicationModel::SuspendingEventArgs const& /* e */)
{
    // TODO: This is the time to save app data in case the process is terminated.
}
void MainPage::App_Suspending(Object^ sender, SuspendingEventArgs^ e)
{
    // TODO: This is the time to save app data in case the process is terminated.
}

釋放資源

您應該釋放獨佔資源和檔案控制代碼,讓其他應用程式可以在您的應用程式暫停時存取它們。 獨佔資源的範例包括攝影機、I/O 裝置、外部裝置和網路資源。 明確釋放獨佔資源和檔案控制代碼,有助於確保其他應用程式可在您的應用程式暫停時存取它們。 當應用程式繼續時,它應該重新取得其獨佔資源和檔案控制代碼。

備註

每當使用者切換到另一個應用程式或桌面或 [開始] 畫面時,系統就會暫停您的應用程式。 每當使用者切換回應用程式,系統就會恢復您的應用程式。 當系統恢復您的應用程式時,變數和資料結構的內容會與系統暫停應用程式之前的內容相同。 系統會將應用程式還原到它離開時的確切位置,讓使用者看起來就像是在背景中執行一樣。

系統會在應用程式暫停時嘗試將應用程式及其資料保留在記憶體中。 不過,如果系統沒有將應用程式保留在記憶體中的資源,系統將會終止您的應用程式。 當使用者切換回已終止的暫停應用程式時,系統會傳送 Activated 事件,並在 OnLaunched 方法中還原其應用程式資料。

系統不會在應用程式終止時通知應用程式,因此您的應用程式必須儲存其應用程式資料,並在暫停時釋放獨佔資源和檔案控制代碼,並在終止後啟動應用程式時加以還原。

如果您在處理常式內進行非同步呼叫,控制項會立即從該非同步呼叫傳回。 這表示,即使非同步呼叫尚未完成,執行狀態仍可從事件處理常式傳回,而且您的應用程式仍會移至下一個狀態。 使用傳遞給您的事件處理常式的 EnteredBackgroundEventArgs 物件上的 GetDeferral 方法,將暫停延遲到在返回的 Windows.Foundation.Deferral 物件上呼叫 Complete 方法之後。

延遲不會增加在應用程式終止之前執行程式碼的時間量。 它只會延遲終止,直到呼叫該延遲的 Complete 方法,或者截止期限到期 (以先到者為準)。 若要延長暫停狀態中的時間,請使用 ExtendedExecutionSession

注意

為了改善 Windows 8.1 中的系統回應性,應用程式在暫停資源之後,會給予低優先順序的資源存取權。 為了支援這個新的優先順序,會延長暫停作業逾時,讓應用程式在 Windows 上具有相當於正常優先順序的 5 秒逾時,或在 Windows Phone 上為 1 到 10 秒。 您無法延長或改變此逾時期限。

關於使用 Visual Studio 進行偵錯的注意事項:Visual Studio 會防止 Windows 暫停附加至偵錯工具的應用程式。 這是要讓使用者在執行應用程式時檢視 Visual Studio 偵錯 UI。 當您偵錯應用程式時,可以使用 Visual Studio 將暫停事件傳送給應用程式。 確定畫面顯示 [偵錯位置] 工具列,然後按一下 [暫停] 圖示。