[PT Run] Closing PT Run and new settings app when PowerToys is not running (#3853)

Closing PowerLauncher, FZ Editor and new settings app when PowerToys is not running
This commit is contained in:
Yevhenii Holovachov
2020-06-10 20:58:34 +03:00
committed by GitHub
parent 38350a1ae4
commit 6fdfd3b9e7
7 changed files with 111 additions and 6 deletions

View File

@@ -3,6 +3,9 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
using interop;
using Windows.UI.Popups;
@@ -21,6 +24,8 @@ namespace Microsoft.PowerToys.Settings.UI.Runner
public static bool IsUserAnAdmin { get; set; }
public static int PowerToysPID { get; set; }
[STAThread]
public static void Main(string[] args)
{
@@ -31,6 +36,9 @@ namespace Microsoft.PowerToys.Settings.UI.Runner
if (args.Length >= ArgumentsQty)
{
int.TryParse(args[2], out int powerToysPID);
PowerToysPID = powerToysPID;
if (args[4] == "true")
{
IsElevated = true;
@@ -49,6 +57,8 @@ namespace Microsoft.PowerToys.Settings.UI.Runner
IsUserAnAdmin = false;
}
WaitForPowerToysRunner();
ipcmanager = new TwoWayPipeMessageIPCManaged(args[1], args[0], null);
ipcmanager.Start();
app.Run();
@@ -68,5 +78,27 @@ namespace Microsoft.PowerToys.Settings.UI.Runner
{
return ipcmanager;
}
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
internal static void WaitForPowerToysRunner()
{
Task.Run(() =>
{
const uint INFINITE = 0xFFFFFFFF;
const uint WAIT_OBJECT_0 = 0x00000000;
const uint SYNCHRONIZE = 0x00100000;
IntPtr powerToysProcHandle = OpenProcess(SYNCHRONIZE, false, PowerToysPID);
if (WaitForSingleObject(powerToysProcHandle, INFINITE) == WAIT_OBJECT_0)
{
Environment.Exit(0);
}
});
}
}
}