Skip to content

Commit ad5e48b

Browse files
Update DecideRedirection sample for C# and WPF (#5735)
* Update DecideRedirection sample for C# * Update hub/apps/windows-app-sdk/applifecycle/applifecycle-instancing.md Co-authored-by: Copilot <[email protected]> * Update hub/apps/windows-app-sdk/applifecycle/applifecycle-instancing.md Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent b3938e9 commit ad5e48b

1 file changed

Lines changed: 27 additions & 36 deletions

File tree

hub/apps/windows-app-sdk/applifecycle/applifecycle-instancing.md

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -336,56 +336,47 @@ Unlike the UWP version of `RedirectActivationTo`, the Windows App SDK's implemen
336336

337337
### Redirection without blocking
338338

339-
Most apps will want to redirect as early as possible, before doing unnecessary initialization work. For some app types, initialization logic runs on an STA thread, which must not be blocked. AppInstance.RedirectActivationToAsync method is asynchronous, and the calling app must wait for the method to complete, otherwise the redirection will fail. However, waiting on an async call will block the STA. In these situations, call RedirectActivationToAsync in another thread, and set an event when the call completes. Then wait on that event using non-blocking APIs such as CoWaitForMultipleObjects. Here’s a C# sample for a WPF app.
339+
Most apps will want to redirect as early as possible, before doing unnecessary initialization work. For some app types, initialization logic runs on an STA thread, which must not be blocked. **AppInstance.RedirectActivationToAsync** method is asynchronous, and the calling app must wait for the method to complete, otherwise the redirection will fail. However, waiting on an async call will block the STA. In these situations, call **RedirectActivationToAsync** in another thread, and set an event when the call completes. Then wait on that event using non-blocking APIs.
340+
341+
The following is a C# sample for a WPF app:
340342

341343
```csharp
342-
private static bool DecideRedirection()
344+
private static bool DecideRedirection(string key)
343345
{
344346
bool isRedirect = false;
345347

346348
// Find out what kind of activation this is.
347349
AppActivationArguments args = AppInstance.GetCurrent().GetActivatedEventArgs();
348350
ExtendedActivationKind kind = args.Kind;
349-
if (kind == ExtendedActivationKind.File)
351+
352+
if (kind == ExtendedActivationKind.Launch)
350353
{
351354
try
352355
{
353-
// This is a file activation: here we'll get the file information,
354-
// and register the file name as our instance key.
355-
if (args.Data is IFileActivatedEventArgs fileArgs)
356+
AppInstance keyInstance = AppInstance.FindOrRegisterForKey(key);
357+
358+
// If we successfully registered the key, we must be the
359+
// only instance running that was activated for this key.
360+
if (keyInstance.IsCurrent)
361+
{
362+
// Hook up the Activated event, to allow for this instance of the app
363+
// getting reactivated as a result of multi-instance redirection.
364+
keyInstance.Activated += OnKeyInstanceActivated;
365+
}
366+
else
356367
{
357-
IStorageItem file = fileArgs.Files[0];
358-
AppInstance keyInstance = AppInstance.FindOrRegisterForKey(file.Name);
368+
isRedirect = true;
359369

360-
// If we successfully registered the file name, we must be the
361-
// only instance running that was activated for this file.
362-
if (keyInstance.IsCurrent)
370+
// Ensure we don't block the STA, by doing the redirect operation
371+
// in another thread, and using an event to signal when it has completed.
372+
var redirectSemaphore = new Semaphore(0, 1);
373+
Task.Run(() =>
363374
{
364-
// Hook up the Activated event, to allow for this instance of the app
365-
// getting reactivated as a result of multi-instance redirection.
366-
keyInstance.Activated += OnActivated;
367-
}
368-
else
369-
{
370-
isRedirect = true;
371-
372-
// Ensure we don't block the STA, by doing the redirect operation
373-
// in another thread, and using an event to signal when it has completed.
374-
redirectEventHandle = CreateEvent(IntPtr.Zero, true, false, null);
375-
if (redirectEventHandle != IntPtr.Zero)
376-
{
377-
Task.Run(() =>
378-
{
379-
keyInstance.RedirectActivationToAsync(args).AsTask().Wait();
380-
SetEvent(redirectEventHandle);
381-
});
382-
uint CWMO_DEFAULT = 0;
383-
uint INFINITE = 0xFFFFFFFF;
384-
_ = CoWaitForMultipleObjects(
385-
CWMO_DEFAULT, INFINITE, 1,
386-
new IntPtr[] { redirectEventHandle }, out uint handleIndex);
387-
}
388-
}
375+
keyInstance.RedirectActivationToAsync(args).AsTask().Wait();
376+
redirectSemaphore.Release();
377+
});
378+
redirectSemaphore.WaitOne();
379+
389380
}
390381
}
391382
catch (Exception ex)

0 commit comments

Comments
 (0)