Skip to content

Free, open-source Windows optimization tool for performance, privacy, and simplicity.

refactor: service process management to use async/await

fatDuck90019d2cbf16
diff --git a/optimizerDuck/Domain/Optimizations/Categories/BloatwareAndServices.cs b/optimizerDuck/Domain/Optimizations/Categories/BloatwareAndServices.cs
index ac7ca38..124176e 100644
--- a/optimizerDuck/Domain/Optimizations/Categories/BloatwareAndServices.cs
+++ b/optimizerDuck/Domain/Optimizations/Categories/BloatwareAndServices.cs
@@ -65,7 +65,7 @@ public class BloatwareAndServices : IOptimizationCategory
)]
public class ConfigureServices : BaseOptimization
{
- public override Task<ApplyResult> ApplyAsync(
+ public override async Task<ApplyResult> ApplyAsync(
IProgress<ProcessingProgress> progress,
OptimizationContext context
)
@@ -328,14 +328,14 @@ public class BloatwareAndServices : IOptimizationCategory
Total = servicesToChange.Count,
}
);
- ServiceProcessService.ChangeServiceStartupType(service);
+ await ServiceProcessService.ChangeServiceStartupTypeAsync(service);
}
context.Logger.LogInformation(
"Optimized service startup for {Count} services",
servicesToChange.Count
);
- return Task.FromResult(CompleteFromScope());
+ return CompleteFromScope();
}
}
}
diff --git a/optimizerDuck/Domain/Optimizations/Categories/SecurityAndPrivacy.cs b/optimizerDuck/Domain/Optimizations/Categories/SecurityAndPrivacy.cs
index abe090c..f6c4010 100644
--- a/optimizerDuck/Domain/Optimizations/Categories/SecurityAndPrivacy.cs
+++ b/optimizerDuck/Domain/Optimizations/Categories/SecurityAndPrivacy.cs
@@ -26,7 +26,7 @@ public class SecurityAndPrivacy : IOptimizationCategory
)]
public class DisableTelemetry : BaseOptimization
{
- public override Task<ApplyResult> ApplyAsync(
+ public override async Task<ApplyResult> ApplyAsync(
IProgress<ProcessingProgress> progress,
OptimizationContext context
)
@@ -112,7 +112,7 @@ public class SecurityAndPrivacy : IOptimizationCategory
Total = 3,
}
);
- ServiceProcessService.ChangeServiceStartupType(
+ await ServiceProcessService.ChangeServiceStartupTypeAsync(
new ServiceItem("DiagTrack", ServiceStartupType.Disabled),
new ServiceItem("dmwappushservice", ServiceStartupType.Disabled),
new ServiceItem("DcpSvc", ServiceStartupType.Disabled),
@@ -159,7 +159,7 @@ public class SecurityAndPrivacy : IOptimizationCategory
context.Logger.LogWarning(ex, "Failed to disable task {Task}", task);
}
- return Task.FromResult(CompleteFromScope());
+ return CompleteFromScope();
}
}
@@ -170,7 +170,7 @@ public class SecurityAndPrivacy : IOptimizationCategory
)]
public class DisableErrorReporting : BaseOptimization
{
- public override Task<ApplyResult> ApplyAsync(
+ public override async Task<ApplyResult> ApplyAsync(
IProgress<ProcessingProgress> progress,
OptimizationContext context
)
@@ -188,7 +188,7 @@ public class SecurityAndPrivacy : IOptimizationCategory
)
);
- ServiceProcessService.ChangeServiceStartupType(
+ await ServiceProcessService.ChangeServiceStartupTypeAsync(
new ServiceItem("WerSvc", ServiceStartupType.Disabled),
new ServiceItem("PcaSvc", ServiceStartupType.Disabled)
);
@@ -216,7 +216,7 @@ public class SecurityAndPrivacy : IOptimizationCategory
context.Logger.LogInformation(
"Disabled Windows Error Reporting and Compatibility Assistant"
);
- return Task.FromResult(CompleteFromScope());
+ return CompleteFromScope();
}
}
diff --git a/optimizerDuck/Domain/Revert/Steps/ServiceRevertStep.cs b/optimizerDuck/Domain/Revert/Steps/ServiceRevertStep.cs
index fcd4973..96786dd 100644
--- a/optimizerDuck/Domain/Revert/Steps/ServiceRevertStep.cs
+++ b/optimizerDuck/Domain/Revert/Steps/ServiceRevertStep.cs
@@ -34,9 +34,9 @@ public class ServiceRevertStep : IRevertStep
);
/// <inheritdoc />
- public Task<bool> ExecuteAsync()
+ public async Task<bool> ExecuteAsync()
{
- var result = ServiceProcessService.ChangeServiceStartupType(
+ var result = await ServiceProcessService.ChangeServiceStartupTypeAsync(
new ServiceItem { Name = ServiceName, StartupType = OriginalStartupType }
);
@@ -46,7 +46,7 @@ public class ServiceRevertStep : IRevertStep
throw new StepExecutionException(error, ServiceProcessService.LastErrorDetail);
}
- return Task.FromResult(true);
+ return true;
}
/// <inheritdoc />
diff --git a/optimizerDuck/Services/Optimization/Providers/ServiceProcessService.cs b/optimizerDuck/Services/Optimization/Providers/ServiceProcessService.cs
index e1e1660..0625d4c 100644
--- a/optimizerDuck/Services/Optimization/Providers/ServiceProcessService.cs
+++ b/optimizerDuck/Services/Optimization/Providers/ServiceProcessService.cs
@@ -26,11 +26,11 @@ public static class ServiceProcessService
internal static string? LastError => _lastError.Value;
internal static string? LastErrorDetail => _lastErrorDetail.Value;
- public static ServiceStartupType? GetStartupType(string serviceName)
+ public static async Task<ServiceStartupType?> GetStartupTypeAsync(string serviceName)
{
try
{
- var (exitCode, stdout, stderr) = RunScExe($"qc \"{serviceName}\"", 15000);
+ var (exitCode, stdout, stderr) = await RunScExeAsync($"qc \"{serviceName}\"", 15000);
if (exitCode != 0)
{
@@ -76,7 +76,7 @@ public static class ServiceProcessService
}
}
- public static bool ChangeServiceStartupType(ServiceItem item)
+ public static async Task<bool> ChangeServiceStartupTypeAsync(ServiceItem item)
{
_lastError.Value = _lastErrorDetail.Value = null;
@@ -89,7 +89,7 @@ public static class ServiceProcessService
try
{
- var originalStartupType = GetStartupType(item.Name);
+ var originalStartupType = await GetStartupTypeAsync(item.Name);
var scType = item.StartupType switch
{
@@ -100,7 +100,7 @@ public static class ServiceProcessService
_ => "demand",
};
- var (exitCode, stdout, stderr) = RunScExe(
+ var (exitCode, stdout, stderr) = await RunScExeAsync(
$"config \"{item.Name}\" start= {scType}",
30000
);
@@ -129,7 +129,7 @@ public static class ServiceProcessService
item.StartupType
);
- ExecutionScope.Track(nameof(ChangeServiceStartupType), true);
+ ExecutionScope.Track(nameof(ChangeServiceStartupTypeAsync), true);
ExecutionScope.RecordStep(
Translations.Service_Service_Name,
description,
@@ -147,14 +147,14 @@ public static class ServiceProcessService
sw.Elapsed.FormatTime(),
item.StartupType
);
- ExecutionScope.Track(nameof(ChangeServiceStartupType), false);
+ ExecutionScope.Track(nameof(ChangeServiceStartupTypeAsync), false);
ExecutionScope.RecordStep(
Translations.Service_Service_Name,
description,
false,
null,
_lastError.Value,
- () => Task.FromResult(ChangeServiceStartupType(item)),
+ () => ChangeServiceStartupTypeAsync(item),
_lastErrorDetail.Value
);
return false;
@@ -174,21 +174,21 @@ public static class ServiceProcessService
item.Name,
item.StartupType
);
- ExecutionScope.Track(nameof(ChangeServiceStartupType), false);
+ ExecutionScope.Track(nameof(ChangeServiceStartupTypeAsync), false);
ExecutionScope.RecordStep(
Translations.Service_Service_Name,
description,
false,
null,
_lastError.Value,
- () => Task.FromResult(ChangeServiceStartupType(item)),
+ () => ChangeServiceStartupTypeAsync(item),
_lastErrorDetail.Value
);
return false;
}
}
- private static (int ExitCode, string Stdout, string Stderr) RunScExe(
+ private static async Task<(int ExitCode, string Stdout, string Stderr)> RunScExeAsync(
string arguments,
int timeoutMs)
{
@@ -208,17 +208,27 @@ public static class ServiceProcessService
process.Start();
var stdoutTask = process.StandardOutput.ReadToEndAsync();
var stderrTask = process.StandardError.ReadToEndAsync();
- process.WaitForExit(timeoutMs);
- Task.WaitAll(stdoutTask, stderrTask);
- return (process.ExitCode, stdoutTask.Result, stderrTask.Result);
+ using var cts = new CancellationTokenSource(timeoutMs);
+ try
+ {
+ await process.WaitForExitAsync(cts.Token);
+ }
+ catch (OperationCanceledException)
+ {
+ try { process.Kill(); } catch { }
+ }
+
+ var stdout = await stdoutTask;
+ var stderr = await stderrTask;
+ return (process.ExitCode, stdout, stderr);
}
/// <summary>Changes the startup type for multiple services.</summary>
/// <param name="items">The service items to update.</param>
- public static void ChangeServiceStartupType(params ServiceItem[] items)
+ public static async Task ChangeServiceStartupTypeAsync(params ServiceItem[] items)
{
foreach (var item in items)
- ChangeServiceStartupType(item);
+ await ChangeServiceStartupTypeAsync(item);
}
}