Skip to content

Portable Tool for an Ideal Windows Setup

fix thread-safety

Greedekse02a395c84e7
diff --git a/.Source/GTweak/Utilities/Configuration/MonitoringSystem.cs b/.Source/GTweak/Utilities/Configuration/MonitoringSystem.cs
index 2a2bdcb3..08c037ff 100644
--- a/.Source/GTweak/Utilities/Configuration/MonitoringSystem.cs
+++ b/.Source/GTweak/Utilities/Configuration/MonitoringSystem.cs
@@ -66,7 +66,7 @@ namespace GTweak.Utilities.Configuration
return (int)((float)(totalMemory - availableMemory) / totalMemory * 100);
}
- internal async void GetTotalProcessorUsageAsync()
+ internal async Task GetTotalProcessorUsage()
{
try
{
@@ -81,7 +81,7 @@ namespace GTweak.Utilities.Configuration
ulong idleTicks = ConvertTimeToTicks(idleTime);
ulong totalTicks = ConvertTimeToTicks(kernelTime) + ConvertTimeToTicks(userTime);
- await Task.Delay(100);
+ await Task.Delay(1000);
if (!GetSystemTimes(out idleTime, out kernelTime, out userTime))
{
diff --git a/.Source/GTweak/Utilities/Configuration/SystemDiagnostics.cs b/.Source/GTweak/Utilities/Configuration/SystemDiagnostics.cs
index dd3c3ca6..cb702742 100644
--- a/.Source/GTweak/Utilities/Configuration/SystemDiagnostics.cs
+++ b/.Source/GTweak/Utilities/Configuration/SystemDiagnostics.cs
@@ -147,9 +147,9 @@ namespace GTweak.Utilities.Configuration
/// If the manufacturer did not provide a serial number and the obtained value is "Default string",
/// then the serial number will not be displayed, similar to the situation with the motherboard.
/// </summary>
- private async void GetBiosInfo()
+ private void GetBiosInfo()
{
- string output = await CommandExecutor.GetCommandOutput("bcdedit");
+ string output = CommandExecutor.GetCommandOutput("bcdedit").GetAwaiter().GetResult();
HardwareData.BiosMode = output.IndexOf("efi", StringComparison.OrdinalIgnoreCase) >= 0 ? "UEFI" : "Legacy Boot";
foreach (var managementObj in new ManagementObjectSearcher(@"root\cimv2", "select Name, Caption, Description, SMBIOSBIOSVersion, SerialNumber from Win32_BIOS", new EnumerationOptions { ReturnImmediately = true }).Get())
@@ -434,7 +434,7 @@ namespace GTweak.Utilities.Configuration
"fr" => "orange.fr",
"es" => "terra.es",
"it" => "libero.it",
- string lang when new[] { "tr", "in", "ar" }.Contains(lang) => "bing.com",
+ string name when new[] { "tr", "in", "ar" }.Contains(name) => "bing.com",
_ => "google.com"
};
@@ -456,50 +456,47 @@ namespace GTweak.Utilities.Configuration
internal void GetUserIpAddress()
{
- Task.Run(async () =>
+ if (IsNetworkAvailable())
{
- if (IsNetworkAvailable())
- {
- using HttpClient client = new HttpClient { Timeout = TimeSpan.FromSeconds(5) };
+ using HttpClient client = new HttpClient { Timeout = TimeSpan.FromSeconds(5) };
- foreach (string url in new[] { "https://ipapi.co/json/", "https://api.db-ip.com/v2/free/self", "http://ip-api.com/json/?fields=61439" })
+ foreach (string url in new[] { "https://ipapi.co/json/", "https://api.db-ip.com/v2/free/self", "http://ip-api.com/json/?fields=61439" })
+ {
+ try
{
- try
- {
- using HttpResponseMessage response = await client.GetAsync(url);
+ using HttpResponseMessage response = client.GetAsync(url).GetAwaiter().GetResult();
- if (!response.IsSuccessStatusCode)
- {
- CurrentConnection = ConnectionStatus.Block;
- continue;
- }
+ if (!response.IsSuccessStatusCode)
+ {
+ CurrentConnection = ConnectionStatus.Block;
+ continue;
+ }
- IPMetadata ipMetadata = IPMetadata.ParseData(await response.Content.ReadAsStringAsync());
+ IPMetadata ipMetadata = IPMetadata.ParseData(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
- if (IPAddress.TryParse(ipMetadata.Ip, out _) && !string.IsNullOrWhiteSpace(ipMetadata?.Ip) && !string.IsNullOrWhiteSpace(ipMetadata?.Country))
- {
- CurrentConnection = ConnectionStatus.Available;
- HardwareData.UserIPAddress = $"{ipMetadata.Ip} ({ipMetadata.Country})";
- break;
- }
- else
- CurrentConnection = ConnectionStatus.Block;
+ if (IPAddress.TryParse(ipMetadata.Ip, out _) && !string.IsNullOrWhiteSpace(ipMetadata?.Ip) && !string.IsNullOrWhiteSpace(ipMetadata?.Country))
+ {
+ CurrentConnection = ConnectionStatus.Available;
+ HardwareData.UserIPAddress = $"{ipMetadata.Ip} ({ipMetadata.Country})";
+ break;
}
- catch { CurrentConnection = ConnectionStatus.Limited; }
+ else
+ CurrentConnection = ConnectionStatus.Block;
}
+ catch { CurrentConnection = ConnectionStatus.Limited; }
}
- else
- CurrentConnection = ConnectionStatus.Lose;
+ }
+ else
+ CurrentConnection = ConnectionStatus.Lose;
- if (new Dictionary<ConnectionStatus, string>
+ if (new Dictionary<ConnectionStatus, string>
{
{ ConnectionStatus.Lose, "connection_lose_systemInformation" },
{ ConnectionStatus.Block, "connection_block_systemInformation" },
{ ConnectionStatus.Limited, "connection_limited_systemInformation" }
}.TryGetValue(CurrentConnection, out string resourceKey)) { HardwareData.UserIPAddress = (string)Application.Current.Resources[resourceKey]; }
- isIPAddressFormatValid = HardwareData.UserIPAddress.Any(char.IsDigit);
- });
+ isIPAddressFormatValid = HardwareData.UserIPAddress.Any(char.IsDigit);
}
internal void ValidateVersionUpdates()
diff --git a/.Source/GTweak/View/DataSystemView.xaml.cs b/.Source/GTweak/View/DataSystemView.xaml.cs
index 22f41987..273f8f19 100644
--- a/.Source/GTweak/View/DataSystemView.xaml.cs
+++ b/.Source/GTweak/View/DataSystemView.xaml.cs
@@ -42,10 +42,10 @@ namespace GTweak.View
if ((int)_time.TotalSeconds % 2 == 0)
{
BackgroundQueue backgroundQueue = new BackgroundQueue();
- await backgroundQueue.QueueTask(delegate
+ await backgroundQueue.QueueTask(async delegate
{
new SystemDiagnostics().UpdatingDevicesData();
- new MonitoringSystem().GetTotalProcessorUsageAsync();
+ await new MonitoringSystem().GetTotalProcessorUsage();
});
AnimationProgressBars();
}
diff --git a/.Source/GTweak/Windows/LoadingWindow.xaml.cs b/.Source/GTweak/Windows/LoadingWindow.xaml.cs
index 50af86be..9dda40fb 100644
--- a/.Source/GTweak/Windows/LoadingWindow.xaml.cs
+++ b/.Source/GTweak/Windows/LoadingWindow.xaml.cs
@@ -58,7 +58,7 @@ namespace GTweak.Windows
() => ExecuteWithLogging(new SystemTweaks().ViewBluetoothStatus, nameof(SystemTweaks.ViewBluetoothStatus))
);
- ExecuteWithLogging(new MonitoringSystem().GetTotalProcessorUsageAsync, nameof(MonitoringSystem.GetTotalProcessorUsageAsync));
+ ExecuteWithLogging(() => new MonitoringSystem().GetTotalProcessorUsage().GetAwaiter().GetResult(), nameof(MonitoringSystem.GetTotalProcessorUsage));
}
private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)