Create JumpList in STA thread as some COM APIs are strictly STA only to avoid sporadic CLR crashes (#10057)

This commit is contained in:
Christoph Bergmeister [MVP]
2019-07-09 13:14:49 -07:00
committed by Dongbo Wang
parent 3141405159
commit ffc3af2b5d
2 changed files with 29 additions and 20 deletions
@@ -4,6 +4,7 @@
using System;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using static Microsoft.PowerShell.ComInterfaces;
@@ -11,7 +12,33 @@ namespace Microsoft.PowerShell
{
internal static class TaskbarJumpList
{
internal static void CreateElevatedEntry(string title)
// Creating a JumpList entry takes around 55ms when the PowerShell process is interactive and
// owns the current window (otherwise it does a fast exit anyway). Since there is no 'GET' like API,
// we always have to execute this call because we do not know if it has been created yet.
// The JumpList does persist as long as the filepath of the executable does not change but there
// could be disruptions to it like e.g. the bi-annual Windows update, we decided to
// not over-optimize this and always create the JumpList as a non-blocking background STA thread instead.
internal static void CreateRunAsAdministratorJumpList()
{
// Some COM APIs are implicitly STA only, therefore the executing thread must run in STA.
var thread = new Thread(() =>
{
try
{
TaskbarJumpList.CreateElevatedEntry(ConsoleHostStrings.RunAsAdministrator);
}
catch (Exception exception)
{
// Due to COM threading complexity there might still be sporadic failures but they can be
// ignored as creating the JumpList is not critical and persists after its first creation.
Debug.Fail($"Creating 'Run as Administrator' JumpList failed. {exception}");
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
private static void CreateElevatedEntry(string title)
{
// Check startupInfo first to know if the current shell is interactive and owns a window before proceeding
// This check is fast (less than 1ms) and allows for quick-exit
@@ -194,25 +194,7 @@ namespace Microsoft.PowerShell
}
#if !UNIX
// Creating a JumpList entry takes around 55ms when the PowerShell process is interactive and
// owns the current window (otherwise it does a fast exit anyway). Since there is no 'GET' like API,
// we always have to execute this call because we do not know if it has been created yet.
// The JumpList does persist as long as the filepath of the executable does not change but there
// could be disruptions to it like e.g. the bi-annual Windows update, we decided to
// not over-optimize this and always create the JumpList as a non-blocking background task instead.
Task.Run(() =>
{
try
{
TaskbarJumpList.CreateElevatedEntry(ConsoleHostStrings.RunAsAdministrator);
}
catch
{
// Sporadic failures have been observed in some environments. Since the JumpList persists once it
// has been registered, there is no harm suppressing the occasional failure.
}
}
);
TaskbarJumpList.CreateRunAsAdministratorJumpList();
#endif
// First check for and handle PowerShell running in a server mode.