From 4bd12ea04c6edf19a5f5b96fba2cb208a3535a66 Mon Sep 17 00:00:00 2001 From: Rose <83477269+AtariDreams@users.noreply.github.com> Date: Sat, 24 Sep 2022 15:28:05 -0400 Subject: [PATCH] Make the Ping method thread-safe So we do not risk blocking the main UI thread --- .../WindowsDriverLocalService.cs | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/CalculatorUITestFramework/WindowsDriverLocalService.cs b/src/CalculatorUITestFramework/WindowsDriverLocalService.cs index 8841c6cb..60fba41d 100644 --- a/src/CalculatorUITestFramework/WindowsDriverLocalService.cs +++ b/src/CalculatorUITestFramework/WindowsDriverLocalService.cs @@ -20,6 +20,7 @@ using System.IO; using System.Net; using System.Net.Http; using System.Runtime.CompilerServices; +using System.Threading.Tasks; namespace CalculatorUITestFramework { @@ -146,20 +147,23 @@ namespace CalculatorUITestFramework { Uri status; Uri service = this.ServiceUrl; - HttpClient httpClient = new HttpClient(); - httpClient.Timeout = this.InitializationTimeout; + using (HttpClient httpClient = new HttpClient()) + { + httpClient.Timeout = this.InitializationTimeout; - if (service.IsLoopback) - { - status = new Uri("http://localhost:" + Convert.ToString(this.Port) + "/status"); + if (service.IsLoopback) + { + status = new Uri("http://localhost:" + Convert.ToString(this.Port) + "/status"); + } + else + { + status = new Uri(service + "/status"); + } + + var httpResponse = Task.Run(() => httpClient.GetAsync(status)).ConfigureAwait(false).GetAwaiter().GetResult(); + + return httpResponse.IsSuccessStatusCode; } - else - { - status = new Uri(service + "/status"); - } - - var httpResponse = httpClient.GetAsync(status); - return httpResponse.Result.IsSuccessStatusCode; } } }