Skip to content

Portable Tool for an Ideal Windows Setup

ref class and method names for better clarity and logical structure.

Greedeks83f23860f56b
diff --git a/.Source/GTweak/App.xaml.cs b/.Source/GTweak/App.xaml.cs
index 44e82902..366980b9 100644
--- a/.Source/GTweak/App.xaml.cs
+++ b/.Source/GTweak/App.xaml.cs
@@ -45,7 +45,7 @@ namespace GTweak
RunGuard.CheckingApplicationCopies();
await RunGuard.CheckingSystemRequirements();
- _systemDataCollector.StartDeviceMonitoring();
+ await _systemDataCollector.StartDeviceMonitoring();
new LoadingWindow().Show();
}
diff --git a/.Source/GTweak/Core/ViewModel/MainWinViewModel.cs b/.Source/GTweak/Core/ViewModel/MainWinViewModel.cs
index bc2834ee..549c0f12 100644
--- a/.Source/GTweak/Core/ViewModel/MainWinViewModel.cs
+++ b/.Source/GTweak/Core/ViewModel/MainWinViewModel.cs
@@ -19,7 +19,7 @@ namespace GTweak.Core.ViewModel
public ObservableCollection<MainWinModel.LanguageItem> Languages { get; } = new ObservableCollection<MainWinModel.LanguageItem>();
private readonly MainWinModel _model = new MainWinModel();
- private readonly SystemDataCollector _systemDataCollector = new SystemDataCollector();
+ private readonly SystemDataCollector _systemDataCollector = new SystemDataCollector();
public object CurrentView
{
diff --git a/.Source/GTweak/GTweak.csproj b/.Source/GTweak/GTweak.csproj
index 96884c20..d74b3870 100644
--- a/.Source/GTweak/GTweak.csproj
+++ b/.Source/GTweak/GTweak.csproj
@@ -153,7 +153,7 @@
<Compile Include="Core\ViewModel\PakagesViewModel.cs" />
<Compile Include="Core\ViewModel\ServicesViewModel.cs" />
<Compile Include="Core\ViewModel\SystemViewModel.cs" />
- <Compile Include="Utilities\Configuration\DiskTypeIdentifier.cs" />
+ <Compile Include="Utilities\Configuration\StorageTypeIdentifier.cs" />
<Compile Include="Utilities\Configuration\HardwareData.cs" />
<Compile Include="Utilities\Helpers\UnlockHandleHelper.cs" />
<Compile Include="Utilities\Maintenance\ClearingMemory.cs" />
diff --git a/.Source/GTweak/Utilities/Configuration/HardwareData.cs b/.Source/GTweak/Utilities/Configuration/HardwareData.cs
index 7c532cf5..f99e0bc5 100644
--- a/.Source/GTweak/Utilities/Configuration/HardwareData.cs
+++ b/.Source/GTweak/Utilities/Configuration/HardwareData.cs
@@ -57,7 +57,7 @@ namespace GTweak.Utilities.Configuration
internal static bool Realtek { get; set; } = default;
}
- internal static class DiskTypeLabels
+ internal static class StorageTypeLabels
{
internal const string Unspecified = "(Unspecified)";
internal const string SCM = "(SCM)";
@@ -71,5 +71,22 @@ namespace GTweak.Utilities.Configuration
internal const string VHD = "(VHD)";
internal const string VHDX = "(VHDX)";
}
+
+ internal static class StorageTypeMappings
+ {
+ internal static readonly (object[] Keys, string Type)[] MediaTypeMap = new (object[] Keys, string Type)[]
+ {
+ (new object[] { (ushort)3, "Removable Media" }, StorageTypeLabels.HDD),
+ (new object[] { (ushort)4, "Fixed hard disk media" }, StorageTypeLabels.SSD),
+ (new object[] { (ushort)5, "Unspecified" }, StorageTypeLabels.SCM)
+ };
+
+ internal static readonly (ushort BusType, string StorageType)[] BusTypeMap = new (ushort, string)[]
+ {
+ (7, StorageTypeLabels.USB),
+ (12, StorageTypeLabels.SD),
+ (17, StorageTypeLabels.NVMe)
+ };
+ }
}
}
\ No newline at end of file
diff --git a/.Source/GTweak/Utilities/Configuration/MonitoringService.cs b/.Source/GTweak/Utilities/Configuration/MonitoringService.cs
index 484af4b3..dc6f93d0 100644
--- a/.Source/GTweak/Utilities/Configuration/MonitoringService.cs
+++ b/.Source/GTweak/Utilities/Configuration/MonitoringService.cs
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
+using System.Linq;
using System.Management;
using System.Runtime.InteropServices;
using System.ServiceProcess;
+using System.Threading;
using System.Threading.Tasks;
using GTweak.Utilities.Controls;
@@ -58,12 +60,14 @@ namespace GTweak.Utilities.Configuration
{
return await Task.Run(() =>
{
- uint capacity = 1024;
+ const uint initialCapacity = 1024;
+ uint capacity = initialCapacity;
+ uint[] buffer = new uint[capacity];
+ bool success = false;
for (int attempt = 0; attempt < 3; attempt++)
{
- uint[] buffer = new uint[capacity];
- bool success = EnumProcesses(buffer, capacity * sizeof(uint), out uint bytesNeeded);
+ success = EnumProcesses(buffer, capacity * sizeof(uint), out uint bytesNeeded);
if (!success)
{
@@ -75,6 +79,7 @@ namespace GTweak.Utilities.Configuration
if (bytesNeeded > capacity * sizeof(uint))
{
capacity = bytesNeeded / sizeof(uint) + 1;
+ buffer = new uint[capacity];
}
continue;
@@ -86,6 +91,7 @@ namespace GTweak.Utilities.Configuration
}
capacity = bytesNeeded / sizeof(uint) + 1;
+ buffer = new uint[capacity];
}
return Process.GetProcesses().Length.ToString();
@@ -94,23 +100,22 @@ namespace GTweak.Utilities.Configuration
internal async Task<string> GetServicesCount()
{
- return await Task.Run(() =>
+ int running = 0;
+
+ await Task.WhenAll(_servicesList.Select(async svc =>
{
- int running = 0;
- Parallel.ForEach(_servicesList, svc =>
+ try
{
- try
+ svc.Refresh();
+ if (svc.Status == ServiceControllerStatus.Running)
{
- svc.Refresh();
- if (svc.Status == ServiceControllerStatus.Running)
- {
- running++;
- }
+ Interlocked.Increment(ref running);
}
- catch (Exception ex) { ErrorLogging.LogDebug(ex); }
- });
- return running.ToString();
- }).ConfigureAwait(false);
+ }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
+ }).ToArray()).ConfigureAwait(false);
+
+ return running.ToString();
}
internal async Task GetPhysicalAvailableMemory()
@@ -172,16 +177,17 @@ namespace GTweak.Utilities.Configuration
}
}
- internal void StartDeviceMonitoring()
+ internal async Task StartDeviceMonitoring()
{
- Parallel.ForEach(new List<(string filter, DeviceType type, string scope)> {
+ await Task.WhenAll(new List<(string filter, DeviceType type, string scope)>
+ {
($"TargetInstance ISA {(SystemDataCollector.isMsftAvailable ? "'MSFT_PhysicalDisk'" : "'Win32_DiskDrive'")}", DeviceType.Storage, SystemDataCollector.isMsftAvailable ? @"root\microsoft\windows\storage" : null),
("TargetInstance ISA 'Win32_SoundDevice'", DeviceType.Audio, null),
- ("TargetInstance ISA 'Win32_NetworkAdapter' AND TargetInstance.NetConnectionStatus IS NOT NULL", DeviceType.Network, null),},
- parameters => { SubscribeToDeviceEvents(parameters.filter, parameters.type, parameters.scope); });
+ ("TargetInstance ISA 'Win32_NetworkAdapter' AND TargetInstance.NetConnectionStatus IS NOT NULL", DeviceType.Network, null)
+ }.Select(device => Task.Run(() => SubscribeToDeviceEvents(device.filter, device.type, device.scope))).ToArray());
}
- private async void SubscribeToDeviceEvents(string filter, DeviceType type, string scope)
+ private async Task SubscribeToDeviceEvents(string filter, DeviceType type, string scope)
{
try
{
diff --git a/.Source/GTweak/Utilities/Configuration/DiskTypeIdentifier.cs b/.Source/GTweak/Utilities/Configuration/StorageTypeIdentifier.cs
similarity index 89%
rename from .Source/GTweak/Utilities/Configuration/DiskTypeIdentifier.cs
rename to .Source/GTweak/Utilities/Configuration/StorageTypeIdentifier.cs
index 102996ea..b2bb34e9 100644
--- a/.Source/GTweak/Utilities/Configuration/DiskTypeIdentifier.cs
+++ b/.Source/GTweak/Utilities/Configuration/StorageTypeIdentifier.cs
@@ -4,7 +4,7 @@ using Microsoft.Win32.SafeHandles;
namespace GTweak.Utilities.Configuration
{
- internal static class DiskTypeIdentifier
+ internal static class StorageTypeIdentifier
{
private const uint IOCTL_STORAGE_QUERY_PROPERTY = 0x2D1400;
private const uint FILE_SHARE_READ = 1;
@@ -90,7 +90,7 @@ namespace GTweak.Utilities.Configuration
if (handle.IsInvalid)
{
- return HardwareData.DiskTypeLabels.Unspecified;
+ return HardwareData.StorageTypeLabels.Unspecified;
}
STORAGE_PROPERTY_QUERY query = new STORAGE_PROPERTY_QUERY
@@ -117,15 +117,15 @@ namespace GTweak.Utilities.Configuration
return descriptor.BusType switch
{
- STORAGE_BUS_TYPE.BusTypeUnknown => HardwareData.DiskTypeLabels.Unspecified,
- STORAGE_BUS_TYPE.BusTypeNvme => HardwareData.DiskTypeLabels.NVMe,
- STORAGE_BUS_TYPE.BusTypeUsb => HardwareData.DiskTypeLabels.USB,
- STORAGE_BUS_TYPE.BusTypeSd => HardwareData.DiskTypeLabels.SD,
- STORAGE_BUS_TYPE.BusTypeVirtual => HardwareData.DiskTypeLabels.VHD,
- STORAGE_BUS_TYPE.BusTypeFileBackedVirtual => HardwareData.DiskTypeLabels.VHDX,
- STORAGE_BUS_TYPE.BusTypeAtapi => HardwareData.DiskTypeLabels.CD,
- STORAGE_BUS_TYPE.BusTypeScsi => HardwareData.DiskTypeLabels.SCSI,
- _ => isSSD ? HardwareData.DiskTypeLabels.SSD : HardwareData.DiskTypeLabels.HDD
+ STORAGE_BUS_TYPE.BusTypeUnknown => HardwareData.StorageTypeLabels.Unspecified,
+ STORAGE_BUS_TYPE.BusTypeNvme => HardwareData.StorageTypeLabels.NVMe,
+ STORAGE_BUS_TYPE.BusTypeUsb => HardwareData.StorageTypeLabels.USB,
+ STORAGE_BUS_TYPE.BusTypeSd => HardwareData.StorageTypeLabels.SD,
+ STORAGE_BUS_TYPE.BusTypeVirtual => HardwareData.StorageTypeLabels.VHD,
+ STORAGE_BUS_TYPE.BusTypeFileBackedVirtual => HardwareData.StorageTypeLabels.VHDX,
+ STORAGE_BUS_TYPE.BusTypeAtapi => HardwareData.StorageTypeLabels.CD,
+ STORAGE_BUS_TYPE.BusTypeScsi => HardwareData.StorageTypeLabels.SCSI,
+ _ => isSSD ? HardwareData.StorageTypeLabels.SSD : HardwareData.StorageTypeLabels.HDD
};
}
}
@@ -134,11 +134,11 @@ namespace GTweak.Utilities.Configuration
Marshal.FreeHGlobal(buffer);
}
- return HardwareData.DiskTypeLabels.Unspecified;
+ return HardwareData.StorageTypeLabels.Unspecified;
}
catch
{
- return HardwareData.DiskTypeLabels.Unspecified;
+ return HardwareData.StorageTypeLabels.Unspecified;
}
}
}
diff --git a/.Source/GTweak/Utilities/Configuration/SystemDataCollector.cs b/.Source/GTweak/Utilities/Configuration/SystemDataCollector.cs
index d2333ca4..ebd0a6dc 100644
--- a/.Source/GTweak/Utilities/Configuration/SystemDataCollector.cs
+++ b/.Source/GTweak/Utilities/Configuration/SystemDataCollector.cs
@@ -54,20 +54,6 @@ namespace GTweak.Utilities.Configuration
internal static bool isIPAddressFormatValid = false, isMsftAvailable = false;
- private static readonly (object[] Keys, string Type)[] MediaTypeMap = new (object[] Keys, string Type)[]
- {
- (new object[] { (ushort)3, "Removable Media" }, DiskTypeLabels.HDD),
- (new object[] { (ushort)4, "Fixed hard disk media" }, DiskTypeLabels.SSD),
- (new object[] { (ushort)5, "Unspecified" }, DiskTypeLabels.SCM)
- };
-
- private static readonly Dictionary<ushort, string> BusTypeMap = new Dictionary<ushort, string>()
- {
- { 7, DiskTypeLabels.USB },
- { 12, DiskTypeLabels.SD },
- { 17, DiskTypeLabels.NVMe }
- };
-
internal static (string Code, string Region) GetCurrentSystemLang()
{
CultureInfo culture = CultureInfo.CurrentUICulture;
@@ -176,7 +162,7 @@ namespace GTweak.Utilities.Configuration
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.UriSource = new Uri(filePath, UriKind.Absolute);
- bmp.DecodePixelWidth = 100;
+ bmp.DecodePixelWidth = 200;
bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
bmp.EndInit();
@@ -439,35 +425,32 @@ namespace GTweak.Utilities.Configuration
static string GetStorageType(object mediaType, string deviceId, ushort busType, string interfaceType)
{
- string storageType = MediaTypeMap.Where(x => x.Keys != null && mediaType != null && x.Keys.Contains(mediaType) && x.Type != null).Select(x => x.Type).FirstOrDefault() ?? DiskTypeLabels.Unspecified;
+ string storageType = StorageTypeMappings.MediaTypeMap.Where(x => x.Keys != null && mediaType != null && x.Keys.Contains(mediaType) && x.Type != null).Select(x => x.Type).FirstOrDefault() ?? StorageTypeLabels.Unspecified;
if (isMsftAvailable)
{
- if (BusTypeMap.TryGetValue(busType, out var busStorageType))
- {
- storageType = busStorageType;
- }
+ storageType = StorageTypeMappings.BusTypeMap.FirstOrDefault(map => map.BusType == busType).StorageType ?? StorageTypeLabels.Unspecified;
}
else
{
- if ((storageType == DiskTypeLabels.Unspecified || storageType == DiskTypeLabels.HDD) && (string.IsNullOrEmpty(interfaceType) || interfaceType.IndexOf("USB", StringComparison.OrdinalIgnoreCase) >= 0))
+ if ((storageType == StorageTypeLabels.Unspecified || storageType == StorageTypeLabels.HDD) && (string.IsNullOrEmpty(interfaceType) || interfaceType.IndexOf("USB", StringComparison.OrdinalIgnoreCase) >= 0))
{
- return DiskTypeLabels.USB;
+ return StorageTypeLabels.USB;
}
}
- if (storageType != DiskTypeLabels.Unspecified)
+ if (storageType != StorageTypeLabels.Unspecified)
{
- string lowLevelType = DiskTypeIdentifier.GetStorageKind(deviceId);
- if (!string.IsNullOrEmpty(lowLevelType) && lowLevelType != DiskTypeLabels.Unspecified)
+ string lowLevelType = StorageTypeIdentifier.GetStorageKind(deviceId);
+ if (!string.IsNullOrEmpty(lowLevelType) && lowLevelType != StorageTypeLabels.Unspecified)
{
return lowLevelType;
}
}
- if (storageType == DiskTypeLabels.Unspecified && !string.IsNullOrEmpty(deviceId))
+ if (storageType == StorageTypeLabels.Unspecified && !string.IsNullOrEmpty(deviceId))
{
- return DiskTypeIdentifier.GetStorageKind(deviceId);
+ return StorageTypeIdentifier.GetStorageKind(deviceId);
}
return storageType;