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

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

# Conflicts:
#	src/Microsoft.PowerShell.ConsoleHost/WindowsTaskbarJumpList/TaskbarJumpList.cs
This commit is contained in:
Travis Plunk
2019-07-09 21:45:01 +00:00
parent 90237466f0
commit f55bf2a024
2 changed files with 30 additions and 20 deletions
@@ -4,13 +4,41 @@
using System;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using static Microsoft.PowerShell.ComInterfaces;
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
@@ -193,25 +193,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.