From b25932247ee482cb8e678204dbfdef2371c7881b Mon Sep 17 00:00:00 2001 From: Inga Lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Wed, 4 Dec 2019 17:40:42 +0300 Subject: [PATCH] Code cleanup --- RadeonResetBugFixService/.editorconfig | 7 ++ RadeonResetBugFixService/Constants.cs | 4 -- .../Devices/DeviceHelper.cs | 27 ++----- .../Devices/KnownDevices.cs | 7 +- RadeonResetBugFixService/MainHandler.cs | 4 +- RadeonResetBugFixService/Program.cs | 49 ++++++------- .../ProjectInstaller.Designer.cs | 1 - RadeonResetBugFixService/ProjectInstaller.cs | 58 +++++++-------- .../RadeonResetBugFixService.csproj | 31 ++++++++ RadeonResetBugFixService/RegistryHelper.cs | 56 +++++++++++++++ .../Tasks/AbstractServiceTask.cs | 72 ++++++++++--------- .../Tasks/DisableAmdVideoTask.cs | 4 +- .../Tasks/DisableBasicDisplayStartupTask.cs | 5 +- .../Tasks/EnableBasicDisplayStartupTask.cs | 5 +- .../Tasks/FixMonitorTask.cs | 5 +- .../Tasks/StopAudioServiceTask.cs | 9 +-- .../ThirdParty/DisableDevice.cs | 9 +-- .../ThirdParty/MonitorChanger.cs | 6 +- .../ThirdParty/ServiceHelpers.cs | 9 +-- .../ThirdParty/ServicePreshutdownHelpers.cs | 6 +- RadeonResetBugFixService/packages.config | 8 +++ 21 files changed, 228 insertions(+), 154 deletions(-) create mode 100644 RadeonResetBugFixService/.editorconfig create mode 100644 RadeonResetBugFixService/RegistryHelper.cs create mode 100644 RadeonResetBugFixService/packages.config diff --git a/RadeonResetBugFixService/.editorconfig b/RadeonResetBugFixService/.editorconfig new file mode 100644 index 0000000..4e94446 --- /dev/null +++ b/RadeonResetBugFixService/.editorconfig @@ -0,0 +1,7 @@ +[*.cs] + +# CA1303: Do not pass literals as localized parameters +dotnet_diagnostic.CA1303.severity = suggestion + +# CA1031: Do not catch general exception types +dotnet_diagnostic.CA1031.severity = suggestion diff --git a/RadeonResetBugFixService/Constants.cs b/RadeonResetBugFixService/Constants.cs index 5efba3b..6a7d25b 100644 --- a/RadeonResetBugFixService/Constants.cs +++ b/RadeonResetBugFixService/Constants.cs @@ -5,9 +5,5 @@ static class Constants { public static TimeSpan ServiceTimeout { get; } = TimeSpan.FromMinutes(5); - - public static string RegistryKeyBasicDisplay { get; } = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\BasicDisplay"; - - public static string RegistryKeySystemControl { get; } = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control"; } } diff --git a/RadeonResetBugFixService/Devices/DeviceHelper.cs b/RadeonResetBugFixService/Devices/DeviceHelper.cs index 92ae405..f3f3f7e 100644 --- a/RadeonResetBugFixService/Devices/DeviceHelper.cs +++ b/RadeonResetBugFixService/Devices/DeviceHelper.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; using System.Management; - using System.Threading; using System.Threading.Tasks; using Contracts; @@ -23,8 +22,12 @@ private static Guid GuidTryParse(string input) { - Guid.TryParse(input, out var result); - return result; + if (Guid.TryParse(input, out var result)) + { + return result; + } + + return default; } private static DeviceInfo ConvertDeviceInfo(PropertyDataCollection deviceProperties) @@ -93,25 +96,9 @@ private static void RunWithTimeout(Action action, TimeSpan timeout) { - Exception localException = null; Task.WaitAny( - Task.Run(() => - { - try - { - action(); - } - catch (Exception e) - { - localException = new Exception("Exception from action", e); - } - }), + Task.Run(action), Task.Delay(timeout)); - - if (localException != null) - { - throw localException; - } } } } diff --git a/RadeonResetBugFixService/Devices/KnownDevices.cs b/RadeonResetBugFixService/Devices/KnownDevices.cs index 814a933..20974b1 100644 --- a/RadeonResetBugFixService/Devices/KnownDevices.cs +++ b/RadeonResetBugFixService/Devices/KnownDevices.cs @@ -1,18 +1,19 @@ namespace RadeonResetBugFixService.Devices { + using System; using Contracts; static class KnownDevices { public static bool IsAmdVideo(DeviceInfo device) { - return (device.Manufacturer.ToLowerInvariant() == "amd" || device.Manufacturer.ToLowerInvariant().Contains("advanced micro devices")) && - (device.Service.ToLowerInvariant() == "hdaudbus" || device.ClassName.ToLowerInvariant() == "display"); + return ((device.Manufacturer.Equals("AMD", StringComparison.OrdinalIgnoreCase) || device.Manufacturer.IndexOf("Advanced Micro Devices", StringComparison.OrdinalIgnoreCase) >= 0) && + (device.Service.Equals("hdaudbus", StringComparison.OrdinalIgnoreCase) || device.ClassName.Equals("display", StringComparison.OrdinalIgnoreCase))); } public static bool IsVirtualVideo(DeviceInfo device) { - return device.Service.ToLowerInvariant() == "hypervideo"; + return device.Service.Equals("hypervideo", StringComparison.OrdinalIgnoreCase); } } } diff --git a/RadeonResetBugFixService/MainHandler.cs b/RadeonResetBugFixService/MainHandler.cs index 95f6a40..fcb7fd2 100644 --- a/RadeonResetBugFixService/MainHandler.cs +++ b/RadeonResetBugFixService/MainHandler.cs @@ -50,10 +50,10 @@ new ITask[] { new EnableBasicDisplayStartupTask(), - new SleepTask(TimeSpan.FromSeconds(20)), + new SleepTask(TimeSpan.FromSeconds(40)), new EnableAmdVideoTask(this.StartupDevicesStatus), new DisableVirtualVideoTask(this.StartupDevicesStatus), - new SleepTask(TimeSpan.FromSeconds(40)), + new SleepTask(TimeSpan.FromSeconds(20)), new FixMonitorTask(), new DisableVirtualVideoTask(this.StartupDevicesStatus), new FixMonitorTask(), diff --git a/RadeonResetBugFixService/Program.cs b/RadeonResetBugFixService/Program.cs index 713cea2..8d5b63d 100644 --- a/RadeonResetBugFixService/Program.cs +++ b/RadeonResetBugFixService/Program.cs @@ -1,6 +1,5 @@ namespace RadeonResetBugFixService { - using Microsoft.Win32; using System; using System.Security.Principal; using System.ServiceProcess; @@ -13,6 +12,11 @@ /// public static int Main(string[] args) { + if (args == null) + { + throw new ArgumentNullException(nameof(args)); + } + if (Environment.UserInteractive) { if (!HasAdministratorPrivileges()) @@ -52,23 +56,21 @@ return; } - switch (args[0].ToLowerInvariant()) + var command = args[0]; + if (command.Equals("install", StringComparison.OrdinalIgnoreCase)) { + DoInstall(); + } + else if (command.Equals("uninstall", StringComparison.OrdinalIgnoreCase)) + { + DoUninstall(); + } + else if (command.Equals("startup", StringComparison.OrdinalIgnoreCase)) { - case "install": - DoInstall(); - return; - case "uninstall": - DoUninstall(); - return; - case "startup": - DoStartup(); - return; - case "shutdown": - DoShutdown(); - return; - default: - ShowHelp(); - return; + DoStartup(); + } + else if (command.Equals("shutdown", StringComparison.OrdinalIgnoreCase)) + { + DoShutdown(); } } @@ -90,26 +92,17 @@ { Console.WriteLine("Setting registry values..."); - // Prevent Windows from killing services that take up to 300 seconds to shutdown - Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control", "WaitToKillServiceTimeout", (int)Constants.ServiceTimeout.TotalMilliseconds, RegistryValueKind.String); - - // Disable fast restart - Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Power", "HiberbootEnabled", 0, RegistryValueKind.DWord); - - // Allow interactive services (FixMonitorTask only works correctly in interactive mode) - Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windows", "NoInteractiveServices", 0, RegistryValueKind.DWord); - Console.WriteLine("Installing service..."); ServiceHelpers.InstallService(nameof(RadeonResetBugFixService), typeof(RadeonResetBugFixService)); Console.WriteLine("Starting service..."); - ServiceHelpers.StartService(nameof(RadeonResetBugFixService), typeof(RadeonResetBugFixService)); + ServiceHelpers.StartService(nameof(RadeonResetBugFixService)); Console.WriteLine("Started service"); } private static void DoUninstall() { Console.WriteLine("Stopping service..."); - ServiceHelpers.StopService(nameof(RadeonResetBugFixService), typeof(RadeonResetBugFixService)); + ServiceHelpers.StopService(nameof(RadeonResetBugFixService)); Console.WriteLine("Uninstalling service..."); ServiceHelpers.UninstallService(nameof(RadeonResetBugFixService), typeof(RadeonResetBugFixService)); Console.WriteLine("Uninstalled"); diff --git a/RadeonResetBugFixService/ProjectInstaller.Designer.cs b/RadeonResetBugFixService/ProjectInstaller.Designer.cs index 072a5ce..6d50d6c 100644 --- a/RadeonResetBugFixService/ProjectInstaller.Designer.cs +++ b/RadeonResetBugFixService/ProjectInstaller.Designer.cs @@ -36,7 +36,6 @@ this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem; this.serviceProcessInstaller1.Password = null; this.serviceProcessInstaller1.Username = null; - this.serviceProcessInstaller1.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.serviceProcessInstaller1_AfterInstall); // // serviceInstaller1 // diff --git a/RadeonResetBugFixService/ProjectInstaller.cs b/RadeonResetBugFixService/ProjectInstaller.cs index 355f705..82f7bd2 100644 --- a/RadeonResetBugFixService/ProjectInstaller.cs +++ b/RadeonResetBugFixService/ProjectInstaller.cs @@ -1,10 +1,10 @@ namespace RadeonResetBugFixService { + using System; using System.ComponentModel; using System.Configuration.Install; using System.Linq; using System.Management; - using Microsoft.Win32; [RunInstaller(true)] public partial class ProjectInstaller : System.Configuration.Install.Installer @@ -16,54 +16,44 @@ private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e) { - ManagementObject wmiService = null; - ManagementBaseObject InParam = null; - try - { - wmiService = new ManagementObject($"Win32_Service.Name='{this.serviceInstaller1.ServiceName}'"); - InParam = wmiService.GetMethodParameters("Change"); - InParam["DesktopInteract"] = true; - wmiService.InvokeMethod("Change", InParam, null); - } - finally + Console.WriteLine("Preventing Windows from killing services that take up to 300 seconds to shutdown"); + RegistryHelper.SetWaitToKillServiceTimeout((int)Constants.ServiceTimeout.TotalMilliseconds); + + Console.WriteLine("Disabling fast reboot"); + RegistryHelper.SetFastRebootStatus(false); + + Console.WriteLine("Allowing interactive services"); + RegistryHelper.SetInteractiveServicesStatus(true); + + Console.WriteLine("Configuring service as interactive"); + using (var wmiService = new ManagementObject($"Win32_Service.Name='{this.serviceInstaller1.ServiceName}'")) { - if (InParam != null) - InParam.Dispose(); - if (wmiService != null) - wmiService.Dispose(); + using (var InParam = wmiService.GetMethodParameters("Change")) + { + InParam["DesktopInteract"] = true; + wmiService.InvokeMethod("Change", InParam, null); + } } + Console.WriteLine("Setting preshutdown timeout for service"); ThirdParty.ServicePreshutdownHelpers.ServicePreshutdownHelpers.SetPreShutdownTimeOut(this.serviceInstaller1.ServiceName, (uint)Constants.ServiceTimeout.TotalMilliseconds); - var preshutdownOrder = GetPreshutdownOrder(); + Console.WriteLine("Adding service to preshutdown order"); + var preshutdownOrder = RegistryHelper.GetPreshutdownOrder(); if (!preshutdownOrder.Contains(this.serviceInstaller1.ServiceName)) { - SetPreshutdownOrder(new[] { this.serviceInstaller1.ServiceName }.Concat(preshutdownOrder).ToArray()); + RegistryHelper.SetPreshutdownOrder(new[] { this.serviceInstaller1.ServiceName }.Concat(preshutdownOrder).ToArray()); } } private void serviceInstaller1_AfterUninstall(object sender, InstallEventArgs e) { - var preshutdownOrder = GetPreshutdownOrder(); + Console.WriteLine("Removing service from preshutdown order"); + var preshutdownOrder = RegistryHelper.GetPreshutdownOrder(); if (preshutdownOrder.Contains(this.serviceInstaller1.ServiceName)) { - SetPreshutdownOrder(preshutdownOrder.Where((name) => name != this.serviceInstaller1.ServiceName).ToArray()); + RegistryHelper.SetPreshutdownOrder(preshutdownOrder.Where((name) => name != this.serviceInstaller1.ServiceName).ToArray()); } } - - private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e) - { - - } - - private string[] GetPreshutdownOrder() - { - return (string[])Registry.GetValue(Constants.RegistryKeySystemControl, "PreshutdownOrder", new string[0]); - } - - private void SetPreshutdownOrder(string[] data) - { - Registry.SetValue(Constants.RegistryKeySystemControl, "PreshutdownOrder", data, RegistryValueKind.MultiString); - } } } diff --git a/RadeonResetBugFixService/RadeonResetBugFixService.csproj b/RadeonResetBugFixService/RadeonResetBugFixService.csproj index a8a232b..8b48649 100644 --- a/RadeonResetBugFixService/RadeonResetBugFixService.csproj +++ b/RadeonResetBugFixService/RadeonResetBugFixService.csproj @@ -1,5 +1,10 @@  + + + + + Debug @@ -12,6 +17,8 @@ 512 true true + + AnyCPU @@ -73,6 +80,7 @@ + @@ -94,7 +102,9 @@ + + @@ -104,5 +114,26 @@ RadeonResetBugFixService.cs + + + + + + + + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + + \ No newline at end of file diff --git a/RadeonResetBugFixService/RegistryHelper.cs b/RadeonResetBugFixService/RegistryHelper.cs new file mode 100644 index 0000000..69867d1 --- /dev/null +++ b/RadeonResetBugFixService/RegistryHelper.cs @@ -0,0 +1,56 @@ +namespace RadeonResetBugFixService +{ + using System; + using System.Globalization; + using Microsoft.Win32; + + static class RegistryHelper + { + private class RegistryValuePath + { + public string KeyName { get; } + + public string ValueName { get; } + + public RegistryValuePath(string keyName, string valueName) + { + this.KeyName = keyName; + this.ValueName = valueName; + } + } + + private static RegistryValuePath PreshutdownOrderPath = new RegistryValuePath(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control", "PreshutdownOrder"); + + private static RegistryValuePath WaitToKillServiceTimeoutPath = new RegistryValuePath(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control", "WaitToKillServiceTimeout"); + + private static RegistryValuePath FastRebootPath = new RegistryValuePath(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Power", "HiberbootEnabled"); + + private static RegistryValuePath NoInteractiveServicesPath = new RegistryValuePath(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windows", "NoInteractiveServices"); + + private static RegistryValuePath BasicDisplayStartTypePath = new RegistryValuePath(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\BasicDisplay", "Start"); + + private static T GetValue(RegistryValuePath path, T defaultValue = default) => (T)Registry.GetValue(path.KeyName, path.ValueName, defaultValue); + + private static void SetValue(RegistryValuePath path, T value, RegistryValueKind valueKind) => Registry.SetValue(path.KeyName, path.ValueName, value, valueKind); + + private static void SetValue(RegistryValuePath path, int value) => SetValue(path, value, RegistryValueKind.DWord); + + private static void SetValue(RegistryValuePath path, string value) => SetValue(path, value, RegistryValueKind.String); + + private static void SetValue(RegistryValuePath path, string[] value) => SetValue(path, value, RegistryValueKind.MultiString); + + public static string[] GetPreshutdownOrder() => GetValue(PreshutdownOrderPath, Array.Empty()); + + public static void SetPreshutdownOrder(string[] value) => SetValue(PreshutdownOrderPath, value); + + public static void SetWaitToKillServiceTimeout(int milliseconds) => SetValue(WaitToKillServiceTimeoutPath, milliseconds.ToString(CultureInfo.InvariantCulture)); + + public static void SetFastRebootStatus(bool isEnabled) => SetValue(FastRebootPath, isEnabled ? 1 : 0); + + public static void SetInteractiveServicesStatus(bool areInteractiveServicesAllowed) => SetValue(NoInteractiveServicesPath, areInteractiveServicesAllowed ? 0 : 1); + + public static int GetBasicDisplayStartType() => GetValue(BasicDisplayStartTypePath, -1); + + public static void SetBasicDisplayStartType(int startType) => SetValue(BasicDisplayStartTypePath, startType); + } +} diff --git a/RadeonResetBugFixService/Tasks/AbstractServiceTask.cs b/RadeonResetBugFixService/Tasks/AbstractServiceTask.cs index 621233b..fb3eb4b 100644 --- a/RadeonResetBugFixService/Tasks/AbstractServiceTask.cs +++ b/RadeonResetBugFixService/Tasks/AbstractServiceTask.cs @@ -20,57 +20,61 @@ if (this.ShouldStart(originalService)) { - var service = new ServiceController(originalService.ServiceName); - if (service.Status == ServiceControllerStatus.Running) + using (var service = new ServiceController(originalService.ServiceName)) { - logger.Log($"{serviceDescription} is already running"); - } - else - { - if (service.Status != ServiceControllerStatus.StartPending) - { - logger.Log($"Starting service {serviceDescription}"); - service.Start(); - logger.Log($"Initiated service start for"); - } - - logger.Log($"Waiting for service {serviceDescription} to start"); - service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(30)); if (service.Status == ServiceControllerStatus.Running) { - logger.Log($"Service is running"); + logger.Log($"{serviceDescription} is already running"); } else { - logger.Log($"Failed; service state is {service.Status}"); + if (service.Status != ServiceControllerStatus.StartPending) + { + logger.Log($"Starting service {serviceDescription}"); + service.Start(); + logger.Log($"Initiated service start for"); + } + + logger.Log($"Waiting for service {serviceDescription} to start"); + service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(30)); + if (service.Status == ServiceControllerStatus.Running) + { + logger.Log($"Service is running"); + } + else + { + logger.Log($"Failed; service state is {service.Status}"); + } } } } else if (this.ShouldStop(originalService)) { - var service = new ServiceController(originalService.ServiceName); - if (service.Status == ServiceControllerStatus.Stopped) + using (var service = new ServiceController(originalService.ServiceName)) { - logger.Log($"{serviceDescription} is already stopped"); - } - else - { - if (service.Status != ServiceControllerStatus.StopPending) - { - logger.Log($"Stopping service {serviceDescription}"); - service.Stop(); - logger.Log($"Initiated service stop"); - } - - logger.Log($"Waiting for service {serviceDescription} to stop"); - service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(15)); if (service.Status == ServiceControllerStatus.Stopped) { - logger.Log($"Service is stopped"); + logger.Log($"{serviceDescription} is already stopped"); } else { - logger.Log($"Failed; service state is {service.Status}"); + if (service.Status != ServiceControllerStatus.StopPending) + { + logger.Log($"Stopping service {serviceDescription}"); + service.Stop(); + logger.Log($"Initiated service stop"); + } + + logger.Log($"Waiting for service {serviceDescription} to stop"); + service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(15)); + if (service.Status == ServiceControllerStatus.Stopped) + { + logger.Log($"Service is stopped"); + } + else + { + logger.Log($"Failed; service state is {service.Status}"); + } } } } diff --git a/RadeonResetBugFixService/Tasks/DisableAmdVideoTask.cs b/RadeonResetBugFixService/Tasks/DisableAmdVideoTask.cs index 021136f..ff977c8 100644 --- a/RadeonResetBugFixService/Tasks/DisableAmdVideoTask.cs +++ b/RadeonResetBugFixService/Tasks/DisableAmdVideoTask.cs @@ -1,6 +1,4 @@ -using RadeonResetBugFixService.Contracts; - -namespace RadeonResetBugFixService.Tasks +namespace RadeonResetBugFixService.Tasks { using Contracts; using Devices; diff --git a/RadeonResetBugFixService/Tasks/DisableBasicDisplayStartupTask.cs b/RadeonResetBugFixService/Tasks/DisableBasicDisplayStartupTask.cs index c9a1b87..6456de3 100644 --- a/RadeonResetBugFixService/Tasks/DisableBasicDisplayStartupTask.cs +++ b/RadeonResetBugFixService/Tasks/DisableBasicDisplayStartupTask.cs @@ -1,7 +1,6 @@ namespace RadeonResetBugFixService.Tasks { using System; - using Microsoft.Win32; using Contracts; using Devices; @@ -31,9 +30,9 @@ if (disabledStatus.HasValue && !disabledStatus.Value) { logger.Log($"Device is enabled; disabling BasicDisplay service"); - var originalValue = Registry.GetValue(Constants.RegistryKeyBasicDisplay, "Start", -1); + var originalValue = RegistryHelper.GetBasicDisplayStartType(); logger.Log($"Original start value {originalValue}"); - Registry.SetValue(Constants.RegistryKeyBasicDisplay, "Start", 4); + RegistryHelper.SetBasicDisplayStartType(4); } else { diff --git a/RadeonResetBugFixService/Tasks/EnableBasicDisplayStartupTask.cs b/RadeonResetBugFixService/Tasks/EnableBasicDisplayStartupTask.cs index e9226ed..6e5a001 100644 --- a/RadeonResetBugFixService/Tasks/EnableBasicDisplayStartupTask.cs +++ b/RadeonResetBugFixService/Tasks/EnableBasicDisplayStartupTask.cs @@ -1,6 +1,5 @@ namespace RadeonResetBugFixService.Tasks { - using Microsoft.Win32; using Contracts; class EnableBasicDisplayStartupTask : ITask @@ -9,9 +8,9 @@ void ITask.Run(ILogger logger) { - var originalValue = Registry.GetValue(Constants.RegistryKeyBasicDisplay, "Start", -1); + var originalValue = RegistryHelper.GetBasicDisplayStartType(); logger.Log($"Original start value {originalValue}"); - Registry.SetValue(Constants.RegistryKeyBasicDisplay, "Start", 3); + RegistryHelper.SetBasicDisplayStartType(3); } } } diff --git a/RadeonResetBugFixService/Tasks/FixMonitorTask.cs b/RadeonResetBugFixService/Tasks/FixMonitorTask.cs index 01a6517..8e12ab3 100644 --- a/RadeonResetBugFixService/Tasks/FixMonitorTask.cs +++ b/RadeonResetBugFixService/Tasks/FixMonitorTask.cs @@ -1,9 +1,8 @@ namespace RadeonResetBugFixService.Tasks { + using System; using System.Collections.Generic; - using System.Linq; using Contracts; - using Devices; class FixMonitorTask : ITask { @@ -16,7 +15,7 @@ foreach (var display in ThirdParty.MonitorChanger.Display.GetDisplayList()) { logger.Log($"Found display(ID='{display.DeviceID}' Key='{display.DeviceKey}', Name='{display.DeviceName}', String='{display.DeviceString}')"); - if (display.DeviceID.ToLowerInvariant().StartsWith(@"monitor\mhs062e")) + if (display.DeviceID.StartsWith(@"Monitor\MHS062E", StringComparison.OrdinalIgnoreCase)) { hypervDisplays.Add(display); } diff --git a/RadeonResetBugFixService/Tasks/StopAudioServiceTask.cs b/RadeonResetBugFixService/Tasks/StopAudioServiceTask.cs index fcbb3c7..a304377 100644 --- a/RadeonResetBugFixService/Tasks/StopAudioServiceTask.cs +++ b/RadeonResetBugFixService/Tasks/StopAudioServiceTask.cs @@ -1,14 +1,15 @@ -using System.ServiceProcess; - -namespace RadeonResetBugFixService.Tasks +namespace RadeonResetBugFixService.Tasks { + using System; + using System.ServiceProcess; + class StopAudioServiceTask : AbstractServiceTask { public override string TaskName => "Stopping audio service"; protected override bool ShouldStop(ServiceController serviceInfo) { - return serviceInfo.ServiceName.ToLowerInvariant() == "audiosrv"; + return serviceInfo.ServiceName.Equals("audiosrv", StringComparison.OrdinalIgnoreCase); } } } diff --git a/RadeonResetBugFixService/ThirdParty/DisableDevice.cs b/RadeonResetBugFixService/ThirdParty/DisableDevice.cs index 4c4dc1b..e89b9cd 100644 --- a/RadeonResetBugFixService/ThirdParty/DisableDevice.cs +++ b/RadeonResetBugFixService/ThirdParty/DisableDevice.cs @@ -1,8 +1,9 @@ -namespace RadeonResetBugFixService.ThirdParty.DisableDevice +// This file isn't generated, but this comment is necessary to exclude it from StyleCop analysis. +// +// Code taken from https://stackoverflow.com/a/1610140 +// Code for obtaining device status is mine +namespace RadeonResetBugFixService.ThirdParty.DisableDevice { - // Code taken from https://stackoverflow.com/a/1610140 - // Code for obtaining device status is mine - using System; using System.Text; using System.Collections.Generic; diff --git a/RadeonResetBugFixService/ThirdParty/MonitorChanger.cs b/RadeonResetBugFixService/ThirdParty/MonitorChanger.cs index 10d92ec..605e311 100644 --- a/RadeonResetBugFixService/ThirdParty/MonitorChanger.cs +++ b/RadeonResetBugFixService/ThirdParty/MonitorChanger.cs @@ -1,6 +1,8 @@ -namespace RadeonResetBugFixService.ThirdParty.MonitorChanger +// This file isn't generated, but this comment is necessary to exclude it from StyleCop analysis. +// +// Code taken from https://github.com/Grunge/setDisplayRes +namespace RadeonResetBugFixService.ThirdParty.MonitorChanger { - // Code taken from https://github.com/Grunge/setDisplayRes using System; using System.Collections.Generic; using System.Runtime.InteropServices; diff --git a/RadeonResetBugFixService/ThirdParty/ServiceHelpers.cs b/RadeonResetBugFixService/ThirdParty/ServiceHelpers.cs index f9c78b7..eea396b 100644 --- a/RadeonResetBugFixService/ThirdParty/ServiceHelpers.cs +++ b/RadeonResetBugFixService/ThirdParty/ServiceHelpers.cs @@ -92,7 +92,7 @@ } } - public static void StartService(string serviceName, Type serviceType) + public static void StartService(string serviceName) { if (!IsInstalled(serviceName)) return; @@ -101,20 +101,21 @@ if (controller.Status != ServiceControllerStatus.Running) { controller.Start(); - controller.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromMinutes(6)); + controller.WaitForStatus(ServiceControllerStatus.Running, Constants.ServiceTimeout); } } } - public static void StopService(string serviceName, Type serviceType) + public static void StopService(string serviceName) { if (!IsInstalled(serviceName)) return; + using (ServiceController controller = new ServiceController(serviceName)) { if (controller.Status != ServiceControllerStatus.Stopped) { controller.Stop(); - controller.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromMinutes(6)); + controller.WaitForStatus(ServiceControllerStatus.Stopped, Constants.ServiceTimeout); } } } diff --git a/RadeonResetBugFixService/ThirdParty/ServicePreshutdownHelpers.cs b/RadeonResetBugFixService/ThirdParty/ServicePreshutdownHelpers.cs index 5a34f20..9c30632 100644 --- a/RadeonResetBugFixService/ThirdParty/ServicePreshutdownHelpers.cs +++ b/RadeonResetBugFixService/ThirdParty/ServicePreshutdownHelpers.cs @@ -1,6 +1,8 @@ -namespace RadeonResetBugFixService.ThirdParty.ServicePreshutdownHelpers +// This file isn't generated, but this comment is necessary to exclude it from StyleCop analysis. +// +// Code taken from https://social.msdn.microsoft.com/Forums/vstudio/en-US/d14549e2-d0bc-47fb-bb01-7e0ac57fa712/keep-windows-service-alive-for-more-then-3-minutes-when-system-shut-down +namespace RadeonResetBugFixService.ThirdParty.ServicePreshutdownHelpers { - // Code taken from https://social.msdn.microsoft.com/Forums/vstudio/en-US/d14549e2-d0bc-47fb-bb01-7e0ac57fa712/keep-windows-service-alive-for-more-then-3-minutes-when-system-shut-down using System; using System.Runtime.InteropServices; diff --git a/RadeonResetBugFixService/packages.config b/RadeonResetBugFixService/packages.config new file mode 100644 index 0000000..03b42e4 --- /dev/null +++ b/RadeonResetBugFixService/packages.config @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file