check if FZ is running

This commit is contained in:
seraphima
2023-10-27 15:38:40 +02:00
parent d0ebc2d09f
commit 662a2e1715

View File

@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation // Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
@@ -13,12 +13,24 @@ namespace Microsoft.FancyZones.UnitTests.Utils
public class FancyZonesSession : IDisposable public class FancyZonesSession : IDisposable
{ {
private const string FancyZonesPath = @"\..\..\..\PowerToys.FancyZones.exe"; private const string FancyZonesPath = @"\..\..\..\PowerToys.FancyZones.exe";
private const string FancyZonesProcessName = "PowerToys.FancyZones";
private bool stopFancyZones = true;
public Process? FancyZonesProcess { get; } public Process? FancyZonesProcess { get; }
public FancyZonesSession(TestContext testContext) public FancyZonesSession(TestContext testContext)
{ {
try try
{
// Check if FancyZones is already running
Process[] runningFZ = Process.GetProcessesByName(FancyZonesProcessName);
if (runningFZ.Length > 0)
{
FancyZonesProcess = runningFZ[0];
stopFancyZones = false;
}
else
{ {
// Launch FancyZones // Launch FancyZones
string? path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string? path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
@@ -27,6 +39,7 @@ namespace Microsoft.FancyZones.UnitTests.Utils
ProcessStartInfo info = new ProcessStartInfo(path); ProcessStartInfo info = new ProcessStartInfo(path);
FancyZonesProcess = Process.Start(info); FancyZonesProcess = Process.Start(info);
} }
}
catch (Exception ex) catch (Exception ex)
{ {
testContext.WriteLine(ex.Message); testContext.WriteLine(ex.Message);
@@ -39,8 +52,12 @@ namespace Microsoft.FancyZones.UnitTests.Utils
{ {
// Close the application // Close the application
if (FancyZonesProcess != null) if (FancyZonesProcess != null)
{
if (stopFancyZones)
{ {
FancyZonesProcess.Kill(); FancyZonesProcess.Kill();
}
FancyZonesProcess.Close(); FancyZonesProcess.Close();
FancyZonesProcess.Dispose(); FancyZonesProcess.Dispose();
} }