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:
Andrey Nekrasov
2022-09-13 19:25:19 +03:00
committed by GitHub
parent cba6507d2b
commit 09f4dead7f
36 changed files with 258 additions and 480 deletions

View File

@@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.
using System;
using System.ComponentModel.Composition;
using System.Threading;
using System.Windows;
using ColorPicker.Helpers;
@@ -23,8 +24,16 @@ namespace ColorPickerUI
private bool disposedValue;
private ThemeManager _themeManager;
private CancellationTokenSource NativeThreadCTS { get; set; }
[Export]
private static CancellationToken ExitToken { get; set; }
protected override void OnStartup(StartupEventArgs e)
{
NativeThreadCTS = new CancellationTokenSource();
ExitToken = NativeThreadCTS.Token;
_args = e?.Args;
// allow only one instance of color picker
@@ -33,7 +42,7 @@ namespace ColorPickerUI
{
Logger.LogWarning("There is ColorPicker instance running. Exiting Color Picker");
_instanceMutex = null;
Environment.Exit(0);
Shutdown(0);
return;
}
@@ -45,7 +54,8 @@ namespace ColorPickerUI
RunnerHelper.WaitForPowerToysRunner(_powerToysRunnerPid, () =>
{
Logger.LogInfo("PowerToys Runner exited. Exiting ColorPicker");
Environment.Exit(0);
NativeThreadCTS.Cancel();
Dispatcher.Invoke(Shutdown);
});
}
else

View File

@@ -7,7 +7,6 @@ using System.Globalization;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using ColorPicker.Helpers;

View File

@@ -3,11 +3,7 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace ColorPicker.Controls
{

View File

@@ -4,7 +4,6 @@
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

View File

@@ -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 ColorPicker.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();
}
}
}

View File

@@ -7,11 +7,8 @@ using System.ComponentModel.Composition;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using ColorPicker.Telemetry;
using ColorPicker.ViewModelContracts;
using Microsoft.PowerToys.Telemetry;
namespace ColorPicker.Helpers
{

View File

@@ -8,10 +8,7 @@ using System.ComponentModel.Composition;
using System.Windows.Input;
using ColorPicker.Helpers;
using ColorPicker.Settings;
using ColorPicker.Telemetry;
using Microsoft.PowerToys.Settings.UI.Library.Enumerations;
using Microsoft.PowerToys.Settings.UI.Library.Utilities;
using Microsoft.PowerToys.Telemetry;
using static ColorPicker.NativeMethods;
namespace ColorPicker.Keyboard

View File

@@ -5,7 +5,6 @@
using System;
using ColorPicker.Helpers;
using ColorPicker.Mouse;
using ColorPickerUI;
namespace ColorPicker

View File

@@ -2,7 +2,6 @@
// 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.Diagnostics.Tracing;
using Microsoft.PowerToys.Telemetry;
using Microsoft.PowerToys.Telemetry.Events;

View File

@@ -4,7 +4,6 @@
using System;
using System.ComponentModel.Composition;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using System.Windows.Media;
@@ -13,11 +12,9 @@ using ColorPicker.Helpers;
using ColorPicker.Keyboard;
using ColorPicker.Mouse;
using ColorPicker.Settings;
using ColorPicker.Telemetry;
using ColorPicker.ViewModelContracts;
using Common.UI;
using interop;
using Microsoft.PowerToys.Settings.UI.Library.Enumerations;
using Microsoft.PowerToys.Telemetry;
namespace ColorPicker.ViewModels
{
@@ -49,13 +46,24 @@ namespace ColorPicker.ViewModels
ZoomWindowHelper zoomWindowHelper,
AppStateHandler appStateHandler,
KeyboardMonitor keyboardMonitor,
IUserSettings userSettings)
IUserSettings userSettings,
CancellationToken exitToken)
{
_zoomWindowHelper = zoomWindowHelper;
_appStateHandler = appStateHandler;
_userSettings = userSettings;
NativeEventWaiter.WaitForEventLoop(Constants.ShowColorPickerSharedEvent(), _appStateHandler.StartUserSession);
NativeEventWaiter.WaitForEventLoop(Constants.ColorPickerSendSettingsTelemetryEvent(), _userSettings.SendSettingsTelemetry);
NativeEventWaiter.WaitForEventLoop(
Constants.ShowColorPickerSharedEvent(),
_appStateHandler.StartUserSession,
Application.Current.Dispatcher,
exitToken);
NativeEventWaiter.WaitForEventLoop(
Constants.ColorPickerSendSettingsTelemetryEvent(),
_userSettings.SendSettingsTelemetry,
Application.Current.Dispatcher,
exitToken);
if (mouseInfoProvider != null)
{

View File

@@ -2,7 +2,6 @@
// 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.ComponentModel;
using System.Windows;
namespace ColorPicker