You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: hub/apps/windows-app-sdk/applifecycle/applifecycle-instancing.md
+5-4Lines changed: 5 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
description: Describes how to use app instancing features with the app lifecycle API (Windows App SDK).
3
3
title: App instancing with the app lifecycle API (Windows App SDK)
4
4
ms.topic: article
5
-
ms.date: 11/16/2021
5
+
ms.date: 03/07/2024
6
6
keywords: AppLifecycle, Windows, ApplicationModel, instancing, single instance, multi instance
7
7
ms.localizationpriority: medium
8
8
---
@@ -13,15 +13,16 @@ An app's instancing model determines whether multiple instances of your app's pr
13
13
14
14
## Prerequisites
15
15
16
-
17
-
18
16
To use the app lifecycle API in the Windows App SDK:
19
17
20
18
1. Download and install the latest release of the Windows App SDK. For more information, see [Install tools for the Windows App SDK](../set-up-your-development-environment.md).
21
19
2. Follow the instructions to [Create your first WinUI 3 project](../../winui/winui3/create-your-first-winui3-app.md) or to [use the Windows App SDK in an existing project](../use-windows-app-sdk-in-existing-project.md).
22
20
23
21
## Single-instance apps
24
22
23
+
> [!NOTE]
24
+
> For an example of how to implement single instancing in a WinUI 3 app with C#, see [Making the app single-instanced](https://blogs.windows.com/windowsdeveloper/2022/01/28/making-the-app-single-instanced-part-3/) on the Windows developer blog.
25
+
25
26
Apps are single-instanced if there can be only one main process running at a time. Attempting to launch a second instance of a single-instanced app typically results in the first instance's main window being activated instead. Note that this only applies to the main process. Single-instanced apps can create multiple background processes and still be considered single instanced.
26
27
27
28
UWP apps are single-instanced by default. but have the ability to become multi-instanced by deciding at launch-time whether to create an additional instance or activate an existing instance instead.
> Although keys are automatically unregistered when their process terminates, race conditions are possible where another instance may have initiated a redirection to the terminated instance before the terminated instance was unregistered. To mitigate this possibility, an app can use [UnregisterKey](/windows/windows-app-sdk/api/winrt/microsoft.windows.applifecycle.appinstance.unregisterkey) to manually unregister its key before it is terminated, giving the app a chance to redirect activations to another app that is not in the process of exiting.
@@ -15,8 +15,6 @@ The power management APIs use a callback-based model similar to the existing [Po
15
15
16
16
## Prerequisites
17
17
18
-
19
-
20
18
To use the app lifecycle API in the Windows App SDK:
21
19
22
20
1. Download and install the latest release of the Windows App SDK. For more information, see [Install tools for the Windows App SDK](../set-up-your-development-environment.md).
@@ -26,6 +24,8 @@ To use the app lifecycle API in the Windows App SDK:
26
24
27
25
The following example demonstrates how to subscribe and respond to [PowerManager](/windows/windows-app-sdk/api/winrt/microsoft.windows.system.power.powermanager) events. This code subscribes to the [BatteryStatusChanged](/windows/windows-app-sdk/api/winrt/microsoft.windows.system.power.powermanager.batterystatuschanged) event during startup. The app then responds to changes by checking the current power level and adjusting its resource usage appropriately. For example, if the battery discharges at a low power state, the app might defer any non-critical background work.
28
26
27
+
#### [C++/WinRT](#tab/cpp)
28
+
29
29
> [!NOTE]
30
30
> Apps can register and unregister for these events at any time, but most apps will want to set callbacks in `WinMain` that persist as long as the app continues to run.
> Apps can register and unregister for these events at any time, but most apps will want to set callbacks that persist as long as the app continues to run.
OutputFormattedMessage($"Battery status changed: {status}, {remainingCharge}% remaining");
144
+
DetermineWorkloads();
145
+
}
146
+
147
+
privatevoidOnPowerSupplyStatusChanged()
148
+
{
149
+
//...etc
150
+
}
151
+
```
152
+
153
+
---
154
+
99
155
## Configure app logic based on multiple status values
100
156
101
157
[PowerManager](/windows/windows-app-sdk/api/winrt/microsoft.windows.system.power.powermanager) events are relatively low-level, and in some scenarios, a single event handler being called might not provide enough information for the app to decide how to behave. In this example, the [PowerSupplyStatusChanged](/windows/windows-app-sdk/api/winrt/microsoft.windows.system.power.powermanager.powersupplystatuschanged) event could be called when the device is disconnected from power. In that case, the app must check the current battery status before deciding how to proceed.
The [PowerManager](/windows/windows-app-sdk/api/winrt/microsoft.windows.system.power.powermanager) class offers information about other device states relevant to an app's power usage. For example, apps can disable graphics processing when the device's display is turned off.
OutputFormattedMessage($"Display status changed: {status}");
275
+
if (displayStatus==DisplayStatus.Off)
276
+
{
277
+
// The screen is off, let's stop rendering foreground graphics,
278
+
// and instead kick off some background work now.
279
+
StopUpdatingGraphics();
280
+
StartDoingBackgroundWork();
281
+
}
282
+
}
283
+
```
284
+
285
+
---
286
+
168
287
## Unsubscribe from events
169
288
170
289
Apps can register and deregister for notifications during their lifecycle. Use your language's preferred event registration management system if your app doesn't need to receive power status notifications during its entire lifecycle.
@@ -18,12 +18,9 @@ Supporting rich activations requires two steps:
18
18
19
19
## Prerequisites
20
20
21
-
> [!IMPORTANT]
22
-
> The app lifecycle API is currently supported in the [preview release channel](../preview-channel.md) and [experimental release channel](../experimental-channel.md) of the Windows App SDK. This feature is not currently supported for use by apps in production environments.
23
-
24
21
To use the app lifecycle API in the Windows App SDK:
25
22
26
-
1. Download and install the latest preview or experimental release of the Windows App SDK. For more information, see [Install tools for the Windows App SDK](../set-up-your-development-environment.md).
23
+
1. Download and install the latest release of the Windows App SDK. For more information, see [Install tools for the Windows App SDK](../set-up-your-development-environment.md).
27
24
2. Follow the instructions to [Create your first WinUI 3 project](../../winui/winui3/create-your-first-winui3-app.md) or to [use the Windows App SDK in an existing project](../use-windows-app-sdk-in-existing-project.md).
28
25
29
26
## Activation details for unpackaged apps
@@ -32,10 +29,10 @@ The current version of the Windows App SDK supports the four most common activat
|`Launch`| Activate the app from the command line, when the user double-clicks the app's icon, or programmatically via [ShellExecute](/windows/win32/api/shellapi/nf-shellapi-shellexecuteexw) or [CreateProcess](/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw). |
36
-
|`File`| Activate an app that has registered for a file type when a file of the type is opened via [ShellExecute](/windows/win32/api/shellapi/nf-shellapi-shellexecuteexw), [Launcher.LaunchFileAsync](/uwp/api/windows.system.launcher.launchfileasync), or the command line. |
37
-
|`Protocol`| Activate an app that has registered for a protocol when a string of that protocol is executed via [ShellExecute](/windows/win32/api/shellapi/nf-shellapi-shellexecuteexw), [Launcher.LaunchUriAsync](/uwp/api/windows.system.launcher.launchuriasync), or the command-line. |
38
-
|`StartupTask`| Activate the app when the user logs into Windows, either because of a registry key, or because of a shortcut in a well-known startup folder. |
32
+
|`Launch`| Activate the app from the command line, when the user double-clicks the app's icon, or programmatically via [ShellExecute](/windows/win32/api/shellapi/nf-shellapi-shellexecuteexw) or [CreateProcess](/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw). |
33
+
|`File`| Activate an app that has registered for a file type when a file of the type is opened via [ShellExecute](/windows/win32/api/shellapi/nf-shellapi-shellexecuteexw), [Launcher.LaunchFileAsync](/uwp/api/windows.system.launcher.launchfileasync), or the command line. |
34
+
|`Protocol`| Activate an app that has registered for a protocol when a string of that protocol is executed via [ShellExecute](/windows/win32/api/shellapi/nf-shellapi-shellexecuteexw), [Launcher.LaunchUriAsync](/uwp/api/windows.system.launcher.launchuriasync), or the command-line. |
35
+
|`StartupTask`| Activate the app when the user logs into Windows, either because of a registry key, or because of a shortcut in a well-known startup folder. |
39
36
40
37
Each type of unpackaged app retrieves its command line arguments in different ways. For example, C++ Win32 apps expect to receive activation arguments to be passed into `WinMain` in the form of a string (though they also have the option to call [GetCommandLineW](/windows/win32/api/processenv/nf-processenv-getcommandlinew)). Windows Forms apps, however, *must* call [Environment.GetCommandLineArgs](/dotnet/api/system.environment.getcommandlineargs), because arguments will not be automatically passed to them.
0 commit comments