Skip to content

Commit 18fc381

Browse files
yuyoyuppejaimecbernardo
authored andcommitted
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
1 parent 9d8bfea commit 18fc381

36 files changed

+255
-477
lines changed

.github/actions/spell-check/expect.txt

+2-13
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ APeriod
6060
api
6161
APIENTRY
6262
APIIs
63-
Apm
6463
APPBARDATA
6564
appdata
6665
APPICON
@@ -130,7 +129,6 @@ Avanc
130129
Awaitable
131130
awakeness
132131
awakeversion
133-
AWAYMODE
134132
AYUV
135133
backend
136134
backtracer
@@ -498,7 +496,6 @@ dvr
498496
DVSD
499497
DVSL
500498
DVTARGETDEVICE
501-
dwhkl
502499
DWINRT
503500
dwl
504501
dwm
@@ -727,8 +724,6 @@ hhk
727724
HHmmss
728725
HHOOK
729726
hhx
730-
Hiber
731-
Hiberboot
732727
HIBYTE
733728
HICON
734729
HIDEWINDOW
@@ -904,7 +899,6 @@ inheritdoc
904899
initguid
905900
Inkscape
906901
Inlines
907-
Inlining
908902
inorder
909903
INotification
910904
INotify
@@ -1034,7 +1028,6 @@ jxr
10341028
jyuwono
10351029
KBDLLHOOKSTRUCT
10361030
kbm
1037-
KCode
10381031
KEYBDINPUT
10391032
keybindings
10401033
keyboardeventhandlers
@@ -1158,7 +1151,6 @@ lpsz
11581151
lpt
11591152
LPTHREAD
11601153
LPTOP
1161-
lptpm
11621154
LPTSTR
11631155
LPVOID
11641156
LPW
@@ -1223,7 +1215,6 @@ Melman
12231215
memcmp
12241216
memcpy
12251217
memset
1226-
MENUBREAK
12271218
MENUITEMINFO
12281219
MENUITEMINFOW
12291220
Metadatas
@@ -1428,7 +1419,6 @@ ntdll
14281419
NTFS
14291420
NTSTATUS
14301421
nuget
1431-
nuint
14321422
nullonfailure
14331423
nullopt
14341424
nullptr
@@ -1490,7 +1480,6 @@ overlaywindow
14901480
Overridable
14911481
Oversampling
14921482
OWNDC
1493-
OWNERDRAW
14941483
PACL
14951484
pagos
14961485
PAINTSTRUCT
@@ -1532,6 +1521,7 @@ pfo
15321521
pft
15331522
pgp
15341523
pguid
1524+
PHANDLER
15351525
phbm
15361526
phbmp
15371527
phwnd
@@ -1934,6 +1924,7 @@ sse
19341924
ssf
19351925
ssh
19361926
sstream
1927+
stackalloc
19371928
STACKFRAME
19381929
stackoverflow
19391930
stackpanel
@@ -2230,14 +2221,12 @@ VIDEOINFOHEADER
22302221
viewbox
22312222
viewmodel
22322223
vih
2233-
Virt
22342224
virtualization
22352225
Virtualizing
22362226
visiblecolorformats
22372227
Visibletrue
22382228
visualbrush
22392229
visualstudio
2240-
viter
22412230
VKey
22422231
VKTAB
22432232
vmovl

src/common/Common.UI/CustomLibraryThemeProvider.cs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Collections.Generic;
6+
67
using ControlzEx.Theming;
78

89
namespace Common.UI

src/modules/launcher/PowerLauncher/Helper/NativeEventWaiter.cs src/common/Common.UI/NativeEventWaiter.cs

+6-8
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,24 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6-
using System.Reflection;
76
using System.Threading;
8-
using System.Windows;
9-
using Wox.Plugin.Logger;
107

11-
namespace PowerLauncher.Helper
8+
using Dispatcher = System.Windows.Threading.Dispatcher;
9+
10+
namespace Common.UI
1211
{
1312
public static class NativeEventWaiter
1413
{
15-
public static void WaitForEventLoop(string eventName, Action callback, CancellationToken cancel)
14+
public static void WaitForEventLoop(string eventName, Action callback, Dispatcher dispatcher, CancellationToken cancel)
1615
{
1716
new Thread(() =>
1817
{
19-
var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, eventName);
18+
var eventHandle = new EventWaitHandle(false, EventResetMode.ManualReset, eventName);
2019
while (true)
2120
{
2221
if (WaitHandle.WaitAny(new WaitHandle[] { cancel.WaitHandle, eventHandle }) == 1)
2322
{
24-
Log.Info($"Successfully waited for {eventName}", MethodBase.GetCurrentMethod().DeclaringType);
25-
Application.Current.Dispatcher.Invoke(callback);
23+
dispatcher.BeginInvoke(callback);
2624
}
2725
else
2826
{

src/modules/MeasureTool/MeasureToolUI/App.xaml.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using ManagedCommon;
77
using MeasureToolUI.Helpers;
8+
using Microsoft.UI.Dispatching;
89
using Microsoft.UI.Xaml;
910

1011
namespace MeasureToolUI
@@ -36,9 +37,10 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
3637
{
3738
if (int.TryParse(cmdArgs[cmdArgs.Length - 1], out int powerToysRunnerPid))
3839
{
40+
var dispatcher = DispatcherQueue.GetForCurrentThread();
3941
RunnerHelper.WaitForPowerToysRunner(powerToysRunnerPid, () =>
4042
{
41-
Environment.Exit(0);
43+
dispatcher.TryEnqueue(App.Current.Exit);
4244
});
4345
}
4446
}
@@ -51,7 +53,8 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
5153
catch (Exception ex)
5254
{
5355
Logger.LogError($"MeasureToolCore failed to initialize: {ex}");
54-
Environment.Exit(1);
56+
App.Current.Exit();
57+
return;
5558
}
5659

5760
_window = new MainWindow(core);

src/modules/PowerOCR/PowerOCR/App.xaml.cs

+11-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6-
using System.Diagnostics;
76
using System.Threading;
87
using System.Windows;
98
using ManagedCommon;
@@ -23,6 +22,13 @@ public partial class App : Application, IDisposable
2322
private Mutex? _instanceMutex;
2423
private int _powerToysRunnerPid;
2524

25+
private CancellationTokenSource NativeThreadCTS { get; set; }
26+
27+
public App()
28+
{
29+
NativeThreadCTS = new CancellationTokenSource();
30+
}
31+
2632
public void Dispose()
2733
{
2834
GC.SuppressFinalize(this);
@@ -37,7 +43,7 @@ private void Application_Startup(object sender, StartupEventArgs e)
3743
{
3844
Logger.LogWarning("Another running TextExtractor instance was detected. Exiting TextExtractor");
3945
_instanceMutex = null;
40-
Environment.Exit(0);
46+
Shutdown();
4147
return;
4248
}
4349

@@ -51,10 +57,11 @@ private void Application_Startup(object sender, StartupEventArgs e)
5157
RunnerHelper.WaitForPowerToysRunner(_powerToysRunnerPid, () =>
5258
{
5359
Logger.LogInfo("PowerToys Runner exited. Exiting TextExtractor");
54-
Environment.Exit(0);
60+
NativeThreadCTS.Cancel();
61+
Application.Current.Dispatcher.Invoke(() => Shutdown());
5562
});
5663
var userSettings = new UserSettings(new Helpers.ThrottledActionInvoker());
57-
eventMonitor = new EventMonitor();
64+
eventMonitor = new EventMonitor(Application.Current.Dispatcher, NativeThreadCTS.Token);
5865
}
5966
catch (Exception ex)
6067
{

src/modules/PowerOCR/PowerOCR/Helpers/NativeEventWaiter.cs

-29
This file was deleted.

src/modules/PowerOCR/PowerOCR/Keyboard/EventMonitor.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Windows.Interop;
7+
using Common.UI;
78
using interop;
89
using PowerOCR.Helpers;
910
using PowerOCR.Utilities;
@@ -16,9 +17,9 @@ namespace PowerOCR.Keyboard
1617
/// </summary>
1718
internal class EventMonitor
1819
{
19-
public EventMonitor()
20+
public EventMonitor(System.Windows.Threading.Dispatcher dispatcher, System.Threading.CancellationToken exitToken)
2021
{
21-
NativeEventWaiter.WaitForEventLoop(Constants.ShowPowerOCRSharedEvent(), StartOCRSession);
22+
NativeEventWaiter.WaitForEventLoop(Constants.ShowPowerOCRSharedEvent(), StartOCRSession, dispatcher, exitToken);
2223
}
2324

2425
public void StartOCRSession()

src/modules/awake/Awake/Awake.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343

4444
<ItemGroup>
4545
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
46+
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.2.46-beta">
47+
<PrivateAssets>all</PrivateAssets>
48+
</PackageReference>
4649
<PackageReference Include="NLog" Version="4.7.13" />
4750
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20071.2" />
4851
<PackageReference Include="System.Reactive" Version="5.0.0" />

0 commit comments

Comments
 (0)