From f72263331a6d8a04e45fa7a8a4c311c1746d8451 Mon Sep 17 00:00:00 2001 From: Zarana Kiran Desai Date: Thu, 16 May 2024 13:32:04 -0700 Subject: [PATCH 1/3] redirect to the elevated instance if present --- tools/PI/DevHome.PI/Program.cs | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/tools/PI/DevHome.PI/Program.cs b/tools/PI/DevHome.PI/Program.cs index d4505c07a6..35c3857f5b 100644 --- a/tools/PI/DevHome.PI/Program.cs +++ b/tools/PI/DevHome.PI/Program.cs @@ -80,16 +80,35 @@ public static void Main(string[] args) private static async Task DecideRedirection() { - AppInstance instance; var activatedEventArgs = AppInstance.GetCurrent().GetActivatedEventArgs(); - if (RuntimeHelper.IsCurrentProcessRunningAsAdmin()) + var isElevatedInstancePresent = false; + var isUnElevatedInstancePresent = false; + var instanceList = AppInstance.GetInstances(); + foreach (var appInstance in instanceList) + { + if (appInstance.Key.Equals(MainInstanceKey, StringComparison.OrdinalIgnoreCase)) + { + isUnElevatedInstancePresent = true; + } + else if (appInstance.Key.Equals(ElevatedInstanceKey, StringComparison.OrdinalIgnoreCase)) + { + isElevatedInstancePresent = true; + } + } + + AppInstance instance; + if (isElevatedInstancePresent) + { + // Redirect to the elevated instance if present. + instance = AppInstance.FindOrRegisterForKey(ElevatedInstanceKey); + } + else if (RuntimeHelper.IsCurrentProcessRunningAsAdmin()) { // Wait for unelevated instance to exit - var isUnElevatedInstancePresent = false; - do + while (isUnElevatedInstancePresent) { isUnElevatedInstancePresent = false; - var instanceList = AppInstance.GetInstances(); + instanceList = AppInstance.GetInstances(); foreach (var appInstance in instanceList) { if (appInstance.Key.Equals(MainInstanceKey, StringComparison.OrdinalIgnoreCase)) @@ -98,7 +117,6 @@ private static async Task DecideRedirection() } } } - while (isUnElevatedInstancePresent); // Register the elevated instance key instance = AppInstance.FindOrRegisterForKey(ElevatedInstanceKey); From 5a41b11a4d79b004005858406d269b3f5f399a8f Mon Sep 17 00:00:00 2001 From: Zarana Kiran Desai Date: Fri, 17 May 2024 11:25:49 -0700 Subject: [PATCH 2/3] add stopevent --- tools/PI/DevHome.PI/Program.cs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tools/PI/DevHome.PI/Program.cs b/tools/PI/DevHome.PI/Program.cs index 35c3857f5b..f8b5ed02ce 100644 --- a/tools/PI/DevHome.PI/Program.cs +++ b/tools/PI/DevHome.PI/Program.cs @@ -47,6 +47,19 @@ public static void Main(string[] args) .ReadFrom.Configuration(configuration) .CreateLogger(); + var stopEvent = new EventWaitHandle(false, EventResetMode.ManualReset, $"DevHomePI - {Environment.ProcessId}"); + var stopEventThread = new Thread(() => + { + var waitResult = stopEvent.WaitOne(); + + _app?.UIDispatcher?.TryEnqueue(() => + { + var primaryWindow = Application.Current.GetService(); + primaryWindow.Close(); + }); + }); + stopEventThread.Start(); + try { XamlCheckProcessRequirements(); @@ -67,6 +80,11 @@ public static void Main(string[] args) OnActivated(null, AppInstance.GetCurrent().GetActivatedEventArgs()); }); } + + stopEvent.Set(); + stopEventThread.Join(); + stopEvent.Close(); + stopEvent.Dispose(); } catch (Exception ex) { @@ -114,6 +132,8 @@ private static async Task DecideRedirection() if (appInstance.Key.Equals(MainInstanceKey, StringComparison.OrdinalIgnoreCase)) { isUnElevatedInstancePresent = true; + var stopAppInstance = new EventWaitHandle(false, EventResetMode.ManualReset, $"DevHomePI - {appInstance.ProcessId}"); + stopAppInstance.Set(); } } } From a0d601a94eec90a03679415bf7b0b1d62da1eda4 Mon Sep 17 00:00:00 2001 From: Zarana Kiran Desai Date: Fri, 17 May 2024 13:33:14 -0700 Subject: [PATCH 3/3] PR feedback --- tools/PI/DevHome.PI/Program.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tools/PI/DevHome.PI/Program.cs b/tools/PI/DevHome.PI/Program.cs index f8b5ed02ce..7ab01fddca 100644 --- a/tools/PI/DevHome.PI/Program.cs +++ b/tools/PI/DevHome.PI/Program.cs @@ -47,8 +47,8 @@ public static void Main(string[] args) .ReadFrom.Configuration(configuration) .CreateLogger(); - var stopEvent = new EventWaitHandle(false, EventResetMode.ManualReset, $"DevHomePI - {Environment.ProcessId}"); - var stopEventThread = new Thread(() => + var stopEvent = new EventWaitHandle(false, EventResetMode.ManualReset, $"DevHomePI-{Environment.ProcessId}"); + ThreadPool.QueueUserWorkItem((o) => { var waitResult = stopEvent.WaitOne(); @@ -58,7 +58,6 @@ public static void Main(string[] args) primaryWindow.Close(); }); }); - stopEventThread.Start(); try { @@ -81,8 +80,6 @@ public static void Main(string[] args) }); } - stopEvent.Set(); - stopEventThread.Join(); stopEvent.Close(); stopEvent.Dispose(); } @@ -132,7 +129,7 @@ private static async Task DecideRedirection() if (appInstance.Key.Equals(MainInstanceKey, StringComparison.OrdinalIgnoreCase)) { isUnElevatedInstancePresent = true; - var stopAppInstance = new EventWaitHandle(false, EventResetMode.ManualReset, $"DevHomePI - {appInstance.ProcessId}"); + var stopAppInstance = new EventWaitHandle(false, EventResetMode.ManualReset, $"DevHomePI-{appInstance.ProcessId}"); stopAppInstance.Set(); } }