mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 19:57:07 +02:00
[PTRun] Gracefully shutdown all threads when exiting (#20450)
This commit is contained in:
@@ -31,7 +31,10 @@ namespace PowerLauncher
|
||||
{
|
||||
public static PublicAPIInstance API { get; private set; }
|
||||
|
||||
public static CancellationTokenSource NativeThreadCTS { get; private set; }
|
||||
|
||||
private static bool _disposed;
|
||||
|
||||
private PowerToysRunSettings _settings;
|
||||
private MainViewModel _mainVM;
|
||||
private MainWindow _mainWindow;
|
||||
@@ -46,6 +49,8 @@ namespace PowerLauncher
|
||||
[STAThread]
|
||||
public static void Main()
|
||||
{
|
||||
NativeThreadCTS = new CancellationTokenSource();
|
||||
|
||||
Log.Info($"Starting PowerToys Run with PID={Environment.ProcessId}", typeof(App));
|
||||
int powerToysPid = GetPowerToysPId();
|
||||
if (powerToysPid != 0)
|
||||
@@ -67,15 +72,13 @@ namespace PowerLauncher
|
||||
using (var application = new App())
|
||||
{
|
||||
application.InitializeComponent();
|
||||
new Thread(() =>
|
||||
NativeEventWaiter.WaitForEventLoop(
|
||||
Constants.RunExitEvent(),
|
||||
() =>
|
||||
{
|
||||
var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.RunExitEvent());
|
||||
if (eventHandle.WaitOne())
|
||||
{
|
||||
Log.Warn("RunExitEvent was signaled. Exiting PowerToys", typeof(App));
|
||||
ExitPowerToys(application);
|
||||
}
|
||||
}).Start();
|
||||
Log.Warn("RunExitEvent was signaled. Exiting PowerToys", typeof(App));
|
||||
ExitPowerToys(application);
|
||||
}, NativeThreadCTS.Token);
|
||||
|
||||
if (powerToysPid != 0)
|
||||
{
|
||||
@@ -118,8 +121,8 @@ namespace PowerLauncher
|
||||
StringMatcher.Instance = _stringMatcher;
|
||||
_stringMatcher.UserSettingSearchPrecision = _settings.QuerySearchPrecision;
|
||||
|
||||
_mainVM = new MainViewModel(_settings);
|
||||
_mainWindow = new MainWindow(_settings, _mainVM);
|
||||
_mainVM = new MainViewModel(_settings, NativeThreadCTS.Token);
|
||||
_mainWindow = new MainWindow(_settings, _mainVM, NativeThreadCTS.Token);
|
||||
API = new PublicAPIInstance(_settingsVM, _mainVM, _themeManager);
|
||||
_settingsReader = new SettingsReader(_settings, _themeManager);
|
||||
_settingsReader.ReadSettings();
|
||||
@@ -154,14 +157,7 @@ namespace PowerLauncher
|
||||
{
|
||||
SingleInstance<App>.SingleInstanceMutex.Close();
|
||||
|
||||
try
|
||||
{
|
||||
app.Dispose();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Environment.Exit(0);
|
||||
}
|
||||
app.Dispatcher.Invoke(() => app.Shutdown());
|
||||
}
|
||||
|
||||
private static int GetPowerToysPId()
|
||||
@@ -194,6 +190,7 @@ namespace PowerLauncher
|
||||
|
||||
Current.Exit += (s, e) =>
|
||||
{
|
||||
NativeThreadCTS.Cancel();
|
||||
Log.Info("Application.Current.Exit", GetType());
|
||||
Dispose();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user