mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 03:36:44 +02:00
[PowerToys Run] Issues with elevation (#11775)
* Delegate creation of non elevated process * Error handling * nits * Fix potential race condition * Fix spelling * Fix spelling * Fix build
This commit is contained in:
@@ -6,6 +6,7 @@ using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Windows;
|
||||
using interop;
|
||||
using ManagedCommon;
|
||||
@@ -43,34 +44,46 @@ namespace PowerLauncher
|
||||
public static void Main()
|
||||
{
|
||||
Log.Info($"Starting PowerToys Run with PID={Process.GetCurrentProcess().Id}", typeof(App));
|
||||
if (SingleInstance<App>.InitializeAsFirstInstance())
|
||||
int powerToysPid = GetPowerToysPId();
|
||||
if (powerToysPid != 0)
|
||||
{
|
||||
using (var application = new App())
|
||||
{
|
||||
application.InitializeComponent();
|
||||
NativeEventWaiter.WaitForEventLoop(Constants.RunExitEvent(), () =>
|
||||
{
|
||||
Log.Warn("RunExitEvent was signaled. Exiting PowerToys", typeof(App));
|
||||
ExitPowerToys(application);
|
||||
});
|
||||
|
||||
int powerToysPid = GetPowerToysPId();
|
||||
if (powerToysPid != 0)
|
||||
{
|
||||
Log.Info($"Runner pid={powerToysPid}", typeof(App));
|
||||
RunnerHelper.WaitForPowerToysRunner(powerToysPid, () =>
|
||||
{
|
||||
Log.Info($"Runner with pid={powerToysPid} exited. Exiting PowerToys Run", typeof(App));
|
||||
ExitPowerToys(application);
|
||||
});
|
||||
}
|
||||
|
||||
application.Run();
|
||||
}
|
||||
// The process started from the PT Run module interface. One instance is handled there.
|
||||
Log.Info($"Runner pid={powerToysPid}", typeof(App));
|
||||
SingleInstance<App>.CreateInstanceMutex();
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Error("There is already running PowerToys Run instance. Exiting PowerToys Run", typeof(App));
|
||||
// If PT Run is started as standalone application check if there is already running instance
|
||||
if (!SingleInstance<App>.InitializeAsFirstInstance())
|
||||
{
|
||||
Log.Warn("There is already running PowerToys Run instance. Exiting PowerToys Run", typeof(App));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
using (var application = new App())
|
||||
{
|
||||
application.InitializeComponent();
|
||||
new Thread(() =>
|
||||
{
|
||||
var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.RunExitEvent());
|
||||
if (eventHandle.WaitOne())
|
||||
{
|
||||
Log.Warn("RunExitEvent was signaled. Exiting PowerToys", typeof(App));
|
||||
ExitPowerToys(application);
|
||||
}
|
||||
}).Start();
|
||||
|
||||
if (powerToysPid != 0)
|
||||
{
|
||||
RunnerHelper.WaitForPowerToysRunner(powerToysPid, () =>
|
||||
{
|
||||
Log.Info($"Runner with pid={powerToysPid} exited. Exiting PowerToys Run", typeof(App));
|
||||
ExitPowerToys(application);
|
||||
});
|
||||
}
|
||||
|
||||
application.Run();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user