mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-03 17:56:44 +02:00
Don't use Environment.Exit (#20532)
* [Awake] Don't use Process.Exit and move to CsWin32 * [PowerLauncher] Remove unused API * [ColorPicker] Use cancellable NativeEventWaiter + cleanup using * [TextExtractor] Don't use Environment.Exit * [MeasureTool] Don't use Environment.Exit(0); * [FZE] don't use Environment.Exit and fix WaitForPowerToysRunner
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using System.Windows;
|
||||
using ManagedCommon;
|
||||
@@ -23,6 +22,13 @@ public partial class App : Application, IDisposable
|
||||
private Mutex? _instanceMutex;
|
||||
private int _powerToysRunnerPid;
|
||||
|
||||
private CancellationTokenSource NativeThreadCTS { get; set; }
|
||||
|
||||
public App()
|
||||
{
|
||||
NativeThreadCTS = new CancellationTokenSource();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
GC.SuppressFinalize(this);
|
||||
@@ -37,7 +43,7 @@ public partial class App : Application, IDisposable
|
||||
{
|
||||
Logger.LogWarning("Another running TextExtractor instance was detected. Exiting TextExtractor");
|
||||
_instanceMutex = null;
|
||||
Environment.Exit(0);
|
||||
Shutdown();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -51,10 +57,11 @@ public partial class App : Application, IDisposable
|
||||
RunnerHelper.WaitForPowerToysRunner(_powerToysRunnerPid, () =>
|
||||
{
|
||||
Logger.LogInfo("PowerToys Runner exited. Exiting TextExtractor");
|
||||
Environment.Exit(0);
|
||||
NativeThreadCTS.Cancel();
|
||||
Application.Current.Dispatcher.Invoke(() => Shutdown());
|
||||
});
|
||||
var userSettings = new UserSettings(new Helpers.ThrottledActionInvoker());
|
||||
eventMonitor = new EventMonitor();
|
||||
eventMonitor = new EventMonitor(Application.Current.Dispatcher, NativeThreadCTS.Token);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Windows;
|
||||
|
||||
namespace PowerOCR.Helpers
|
||||
{
|
||||
public static class NativeEventWaiter
|
||||
{
|
||||
public static void WaitForEventLoop(string eventName, Action callback)
|
||||
{
|
||||
new Thread(() =>
|
||||
{
|
||||
var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, eventName);
|
||||
while (true)
|
||||
{
|
||||
if (eventHandle.WaitOne())
|
||||
{
|
||||
Logger.LogInfo($"Successfully waited for {eventName}");
|
||||
Application.Current.Dispatcher.Invoke(callback);
|
||||
}
|
||||
}
|
||||
}).Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
using System;
|
||||
using System.Windows.Interop;
|
||||
using Common.UI;
|
||||
using interop;
|
||||
using PowerOCR.Helpers;
|
||||
using PowerOCR.Utilities;
|
||||
@@ -16,9 +17,9 @@ namespace PowerOCR.Keyboard
|
||||
/// </summary>
|
||||
internal class EventMonitor
|
||||
{
|
||||
public EventMonitor()
|
||||
public EventMonitor(System.Windows.Threading.Dispatcher dispatcher, System.Threading.CancellationToken exitToken)
|
||||
{
|
||||
NativeEventWaiter.WaitForEventLoop(Constants.ShowPowerOCRSharedEvent(), StartOCRSession);
|
||||
NativeEventWaiter.WaitForEventLoop(Constants.ShowPowerOCRSharedEvent(), StartOCRSession, dispatcher, exitToken);
|
||||
}
|
||||
|
||||
public void StartOCRSession()
|
||||
|
||||
Reference in New Issue
Block a user