如何在 Visual Studio 中偵測 UWP 應用程式時觸發暫止、繼續和背景事件How to trigger suspend, resume, and background events while debugging UWP apps in Visual Studio

不在偵錯模式時,由 Windows 處理程序生命週期管理 (PLM) 控制您應用程式的執行狀態:啟動、暫停、繼續和終止應用程式,以便回應使用者動作和裝置的狀態。When you are not debugging, Windows Process Lifetime Management (PLM) controls the execution state of your app—starting, suspending, resuming, and terminating the app in response to user actions and the state of the device. 而處於偵錯模式時,Windows 會停用這些啟用事件。When you are debugging, Windows disables these activation events. 本主題說明如何在偵錯工具中引發這些事件。This topic describes how to fire these events in the debugger.

本主題也將說明如何對 背景工作 偵錯。This topic also describes how to debug Background tasks. 背景工作可讓您在背景進程中執行某些作業,即使您的應用程式不在執行中也一樣。Background tasks enable you to perform certain operations in a background process, even when your app is not running. 您可以使用偵錯工具,將您的應用程式置於偵錯模式,接著無須啟動 UI,就能啟動背景工作並對其偵錯。You can use the debugger to put your app in debug mode and then— without starting the UI—start and debug the background task.

如需處理程式生命週期管理和背景工作的詳細資訊,請參閱 啟動、繼續和多任務。For more information about Process Lifetime Management and background tasks, see Launching, resuming, and multitasking.

觸發處理程式生命週期管理事件Trigger Process Lifetime Management events

當使用者切換離開您的應用程式,或當 Windows 進入低電源狀態時,windows 可能會暫停您的應用程式。Windows can suspend your app when the user switches away from it or when Windows enters a low-power state. 您可以回應 Suspending 事件,將相關的應用程式和使用者資料儲存至永久儲存區,以便釋放資源。You can respond to the Suspending event to save relevant app and user data to persistent storage and to release resources. 當應用程式從「 暫停 」狀態繼續時,它會進入「 執行中 」狀態,並從上次暫停的地方繼續進行。When an app is resumed from the Suspended state, it enters the Running state and continues from where it was when it was suspended. 您可以回應 Resuming 事件,還原或重新整理應用程式狀態,以便回收資源。You can respond to the Resuming event to restore or refresh application state and reclaim resources.

Windows 會在記憶體中盡可能保留暫停的應用程式,但如果沒有足夠的資源得以在記憶體中保留這些應用程式,Windows 會終止它們。Although Windows attempts to keep as many suspended apps in memory as possible, Windows can terminate your app if there aren't enough resources to keep it in memory. 使用者也可以明確關閉您的應用程式。A user can also explicitly close your app. 沒有特定事件用來指出使用者已關閉應用程式。There's no special event to indicate that the user has closed an app.

在 Visual Studio 偵錯工具中,您可以手動暫停、繼續和終止您的應用程式,以便對處理生命週期事件進行偵錯。In the Visual Studio debugger, you can manually suspend, resume, and terminate your apps to debug process lifecycle events. 若要對處理生命週期事件進行偵錯:To debug a process lifecycle event:

  1. 在您想要進行偵錯工具的事件處理常式中設定中斷點。Set a breakpoint in the handler of the event that you want to debug.

  2. F5 開始偵錯。Press F5 to start debugging.

  3. 在 [ 偵錯位置 ] 工具列上,選擇您要引發的事件:On the Debug Location toolbar, choose the event that you want to fire:

    暫止、繼續、結束和背景工作

    暫止和終止 會關閉應用程式並結束偵錯工具。Suspend and terminate closes the app and ends the debug session.

觸發背景工作Trigger background tasks

任何應用程式都可以登錄背景工作,以回應特定的系統事件 (即使應用程式並未執行)。Any app can register a background task to respond to certain system events, even when the app is not running. 背景工作不能執行直接更新 UI 的程式碼,但它們可以透過動態磚更新、徽章更新和快顯通知,對使用者顯示資訊。Background tasks can't run code that directly updates the UI; instead, they show information to the user with tile updates, badge updates, and toast notifications. 如需詳細資訊,請參閱 使用背景工作支援您的應用程式For more information, see Supporting your app with background tasks.

您可以從偵錯工具觸發事件,啟動應用程式的背景工作。You can trigger the events that start background tasks for your app from the debugger.

注意

偵錯工具只能觸發不含資料的事件,例如指出裝置狀態變更的事件。The debugger can trigger only those events that do not contain data, such as events that indicate a change of state in the device. 若是需要使用者輸入或其他資料的背景工作,則必須手動觸發。You have to manually trigger background tasks that require user input or other data.

觸發背景工作事件的最實際方法,就是在您的應用程式未執行時觸發。The most realistic way to trigger a background task event is when your app is not running. 不過,也支援在標準偵錯工作階段中觸發事件。However, triggering the event in a standard debugging session is also supported.

從標準的 debug 會話觸發背景工作事件Trigger a background task event from a standard debug session

  1. 在您要偵錯的背景工作程式碼中設定中斷點。Set a breakpoint in the background task code that you want to debug.

  2. F5 開始偵錯。Press F5 to start debugging.

  3. 從 [ 偵錯位置 ] 工具列的事件清單中,選擇您想要啟動的背景工作。From the events list on the Debug Location toolbar, choose the background task that you want to start.

    暫止、繼續、結束和背景工作

當應用程式未執行時觸發背景工作Trigger a background task when the app is not running

  1. 在您要偵錯的背景工作程式碼中設定中斷點。Set a breakpoint in the background task code that you want to debug.

  2. 開啟啟始專案的偵錯屬性頁。Open the debug property page for the start-up project. 在 [方案總管] 中選取專案。In Solution Explorer, select the project. 在 [ 偵錯 ] 功能表上,選擇 [ 屬性]。On the Debug menu, choose Properties.

    針對 c + + 專案,展開 [設定 屬性 ],然後選擇 [ 調試 程式]。For C++ projects, expand Configuration Properties and then choose Debugging.

  3. 執行下列其中一個動作:Do one of the following:

    • 若是 Visual C# 和 Visual Basic 專案,請選擇 [ 不啟動,但在我的程式碼啟動時進行偵錯]。For Visual C# and Visual Basic projects, choose Do not launch, but debug my code when it starts

      C#/VB debug 啟動應用程式屬性C#/VB debug launch application property

    • 若是 c + + 專案,請從 [啟動應用程式] 清單中選擇 []。For C++ projects, choose No from the Launch application list.

      C++/VB 啟動應用程式偵錯工具屬性C++/VB Launch application debug property

  4. F5 將應用程式置入偵錯模式。Press F5 to put the app in debug mode. 請注意,[ 偵錯位置 ] 工具列的 [ 處理 ] 清單會顯示應用程式封裝名稱,表示您正處於偵錯模式。Note the Process list on the Debug Location toolbar displays the app package name to indicate that you are in debug mode.

    背景工作處理序清單Background task Process list

  5. 從 [ 偵錯位置 ] 工具列的事件清單中,選擇您想要啟動的背景工作。From the events list on the Debug Location toolbar, choose the background task that you want to start.

    暫止、繼續、結束和背景工作Suspend, resume, terminate, and background tasks

從已安裝的應用程式觸發處理程式生命週期管理事件和背景工作Trigger Process Lifetime Management events and background tasks from an installed app

使用 [偵測 已安裝的應用程式封裝 ] 對話方塊,即可載入已安裝在偵錯工具中的應用程式。Use the Debug Installed App Package dialog box to load an app that is already installed into the debugger. 例如,您可以將從 Microsoft Store 安裝的應用程式進行偵錯工具,或在您有應用程式的原始程式檔,而不是應用程式的 Visual Studio 專案時,對應用程式進行 debug 錯。For example, you might debug an app that was installed from Microsoft Store, or debug an app when you have the source files for the app, but not a Visual Studio project for the app. [偵測 已安裝的應用程式套件 ] 對話方塊可讓您在 Visual Studio 電腦或遠端裝置上以「偵測模式」啟動應用程式,或將應用程式設定為在「偵測模式」中執行,但不能啟動應用程式。The Debug Installed App Package dialog box allows you to start an app in debug mode on the Visual Studio machine or on a remote device, or to set the app to run in debug mode but not start it. 如需詳細資訊,請參閱 Debug 已安裝的應用程式套件For more information, see Debug an installed app package.

將應用程式載入至偵錯工具後,您就能使用上述任何程序。Once the app is loaded into the debugger, you can use any of the procedures described above.

診斷背景工作啟用錯誤Diagnosing background task activation errors

適用于背景基礎結構的 Windows 事件檢視器中的診斷記錄包含詳細資訊,您可以用來診斷背景工作錯誤並進行疑難排解。The diagnostic logs in Windows Event Viewer for the background infrastructure contain detailed information that you can use to diagnose and troubleshoot background task errors. 若要檢視記錄檔:To view the log:

  1. 開啟 [事件檢視器] 應用程式。Open the Event Viewer application.

  2. 在 [ 動作 ] 窗格中,選擇 [ 檢視 ],並確定已勾選 [ 顯示分析與偵錯記錄檔 ]。In the Actions pane, choose View and make sure Show Analytic and Debug Logs is checked.

  3. 在 [ 事件檢視器 (本機) ] 樹狀目錄中,依序展開 [ 應用程式及服務記錄檔 > Microsoft > Windows > BackgroundTasksInfrastructure 偵錯。On the Event Viewer (Local) tree, expand the nodes Applications and Services Logs > Microsoft > Windows > BackgroundTasksInfrastructure.

  4. 選擇 [ 診斷 ] 記錄檔。Choose the Diagnostic log.

另請參閱See also