Skip to content

Commit f54085f

Browse files
bergmeisteriSazonov
authored andcommitted
Create JumpList in background thread to improve performance (#6985)
* Perform JumpList creation in background thread to improve performance when pwsh owns the window (i.e. when the Jumplist has to be created) * Move JumpList creation to a later stage to allow for more code that might make it exit earlier as suggested in PR review by @iSazonov
1 parent b66013d commit f54085f

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

src/Microsoft.PowerShell.ConsoleHost/WindowsTaskbarJumpList/TaskbarJumpList.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ internal static class TaskbarJumpList
1212
{
1313
internal static void CreateElevatedEntry(string title)
1414
{
15-
// check startupInfo to know if the current shell owns a window before proceeding
15+
// Check startupInfo first to know if the current shell is interactive and owns a window before proceeding
16+
// This check is fast (less than 1ms) and allows for quick-exit
1617
GetStartupInfo(out StartUpInfo startupInfo);
1718
var STARTF_USESHOWWINDOW = 0x00000001;
1819
var SW_HIDE = 0;

src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using Dbg = System.Management.Automation.Diagnostics;
2727
using ConsoleHandle = Microsoft.Win32.SafeHandles.SafeFileHandle;
2828
using System.Management.Automation.Tracing;
29+
using System.Threading.Tasks;
2930
#if LEGACYTELEMETRY
3031
using Microsoft.PowerShell.Telemetry.Internal;
3132
#endif
@@ -120,10 +121,6 @@ internal static int Start(
120121
}
121122
#endif
122123

123-
#if !UNIX
124-
Microsoft.PowerShell.TaskbarJumpList.CreateElevatedEntry(ConsoleHostStrings.RunAsAdministrator);
125-
#endif
126-
127124
// put PSHOME in front of PATH so that calling `powershell` within `powershell` always starts the same running version
128125
string path = Environment.GetEnvironmentVariable("PATH");
129126
if (path != null)
@@ -214,6 +211,16 @@ internal static int Start(
214211
return ExitCodeBadCommandLineParameter;
215212
}
216213

214+
#if !UNIX
215+
// Creating a JumpList entry takes around 55ms when the PowerShell process is interactive and
216+
// owns the current window (otherwise it does a fast exit anyway). Since there is no 'GET' like API,
217+
// we always have to execute this call because we do not know if it has been created yet.
218+
// The JumpList does persist as long as the filepath of the executable does not change but there
219+
// could be disruptions to it like e.g. the bi-annual Windows update, we decided to
220+
// not over-optimize this and always create the JumpList as a non-blocking background task instead.
221+
Task.Run(() => TaskbarJumpList.CreateElevatedEntry(ConsoleHostStrings.RunAsAdministrator));
222+
#endif
223+
217224
// First check for and handle PowerShell running in a server mode.
218225
if (s_cpp.ServerMode)
219226
{

0 commit comments

Comments
 (0)