Skip to content

Commit

Permalink
Don't use Environment.Exit (#20532)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
yuyoyuppe authored and jaimecbernardo committed Sep 13, 2022
1 parent 9d8bfea commit 18fc381
Show file tree
Hide file tree
Showing 36 changed files with 255 additions and 477 deletions.
15 changes: 2 additions & 13 deletions .github/actions/spell-check/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ APeriod
api
APIENTRY
APIIs
Apm
APPBARDATA
appdata
APPICON
Expand Down Expand Up @@ -130,7 +129,6 @@ Avanc
Awaitable
awakeness
awakeversion
AWAYMODE
AYUV
backend
backtracer
Expand Down Expand Up @@ -498,7 +496,6 @@ dvr
DVSD
DVSL
DVTARGETDEVICE
dwhkl
DWINRT
dwl
dwm
Expand Down Expand Up @@ -727,8 +724,6 @@ hhk
HHmmss
HHOOK
hhx
Hiber
Hiberboot
HIBYTE
HICON
HIDEWINDOW
Expand Down Expand Up @@ -904,7 +899,6 @@ inheritdoc
initguid
Inkscape
Inlines
Inlining
inorder
INotification
INotify
Expand Down Expand Up @@ -1034,7 +1028,6 @@ jxr
jyuwono
KBDLLHOOKSTRUCT
kbm
KCode
KEYBDINPUT
keybindings
keyboardeventhandlers
Expand Down Expand Up @@ -1158,7 +1151,6 @@ lpsz
lpt
LPTHREAD
LPTOP
lptpm
LPTSTR
LPVOID
LPW
Expand Down Expand Up @@ -1223,7 +1215,6 @@ Melman
memcmp
memcpy
memset
MENUBREAK
MENUITEMINFO
MENUITEMINFOW
Metadatas
Expand Down Expand Up @@ -1428,7 +1419,6 @@ ntdll
NTFS
NTSTATUS
nuget
nuint
nullonfailure
nullopt
nullptr
Expand Down Expand Up @@ -1490,7 +1480,6 @@ overlaywindow
Overridable
Oversampling
OWNDC
OWNERDRAW
PACL
pagos
PAINTSTRUCT
Expand Down Expand Up @@ -1532,6 +1521,7 @@ pfo
pft
pgp
pguid
PHANDLER
phbm
phbmp
phwnd
Expand Down Expand Up @@ -1934,6 +1924,7 @@ sse
ssf
ssh
sstream
stackalloc
STACKFRAME
stackoverflow
stackpanel
Expand Down Expand Up @@ -2230,14 +2221,12 @@ VIDEOINFOHEADER
viewbox
viewmodel
vih
Virt
virtualization
Virtualizing
visiblecolorformats
Visibletrue
visualbrush
visualstudio
viter
VKey
VKTAB
vmovl
Expand Down
1 change: 1 addition & 0 deletions src/common/Common.UI/CustomLibraryThemeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;

using ControlzEx.Theming;

namespace Common.UI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Reflection;
using System.Threading;
using System.Windows;
using Wox.Plugin.Logger;

namespace PowerLauncher.Helper
using Dispatcher = System.Windows.Threading.Dispatcher;

namespace Common.UI
{
public static class NativeEventWaiter
{
public static void WaitForEventLoop(string eventName, Action callback, CancellationToken cancel)
public static void WaitForEventLoop(string eventName, Action callback, Dispatcher dispatcher, CancellationToken cancel)
{
new Thread(() =>
{
var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, eventName);
var eventHandle = new EventWaitHandle(false, EventResetMode.ManualReset, eventName);
while (true)
{
if (WaitHandle.WaitAny(new WaitHandle[] { cancel.WaitHandle, eventHandle }) == 1)
{
Log.Info($"Successfully waited for {eventName}", MethodBase.GetCurrentMethod().DeclaringType);
Application.Current.Dispatcher.Invoke(callback);
dispatcher.BeginInvoke(callback);
}
else
{
Expand Down
7 changes: 5 additions & 2 deletions src/modules/MeasureTool/MeasureToolUI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using ManagedCommon;
using MeasureToolUI.Helpers;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;

namespace MeasureToolUI
Expand Down Expand Up @@ -36,9 +37,10 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
{
if (int.TryParse(cmdArgs[cmdArgs.Length - 1], out int powerToysRunnerPid))
{
var dispatcher = DispatcherQueue.GetForCurrentThread();
RunnerHelper.WaitForPowerToysRunner(powerToysRunnerPid, () =>
{
Environment.Exit(0);
dispatcher.TryEnqueue(App.Current.Exit);
});
}
}
Expand All @@ -51,7 +53,8 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
catch (Exception ex)
{
Logger.LogError($"MeasureToolCore failed to initialize: {ex}");
Environment.Exit(1);
App.Current.Exit();
return;
}

_window = new MainWindow(core);
Expand Down
15 changes: 11 additions & 4 deletions src/modules/PowerOCR/PowerOCR/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -37,7 +43,7 @@ private void Application_Startup(object sender, StartupEventArgs e)
{
Logger.LogWarning("Another running TextExtractor instance was detected. Exiting TextExtractor");
_instanceMutex = null;
Environment.Exit(0);
Shutdown();
return;
}

Expand All @@ -51,10 +57,11 @@ private void Application_Startup(object sender, StartupEventArgs e)
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)
{
Expand Down
29 changes: 0 additions & 29 deletions src/modules/PowerOCR/PowerOCR/Helpers/NativeEventWaiter.cs

This file was deleted.

5 changes: 3 additions & 2 deletions src/modules/PowerOCR/PowerOCR/Keyboard/EventMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Windows.Interop;
using Common.UI;
using interop;
using PowerOCR.Helpers;
using PowerOCR.Utilities;
Expand All @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions src/modules/awake/Awake/Awake.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@

<ItemGroup>
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.2.46-beta">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="NLog" Version="4.7.13" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20071.2" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
Expand Down
Loading

0 comments on commit 18fc381

Please sign in to comment.