コード例 #1
0
        // 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()
        {
            // The STA apartment state is not supported on NanoServer and Windows IoT.
            // Plus, there is not need to create jump list in those environment anyways.
            if (!Platform.IsWindowsDesktop)
            {
                return;
            }

            // 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}");
                }
            });

            try
            {
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
            }
            catch (System.Threading.ThreadStartException)
            {
                // STA may not be supported on some platforms
            }
        }
コード例 #2
0
        // 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();
        }