From 10d286c8eed0cf31bf99b108a8ff893d5e94ddd5 Mon Sep 17 00:00:00 2001 From: zzyyyl Date: Sat, 23 Sep 2023 23:49:10 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E9=83=A8=E5=88=86=20?= =?UTF-8?q?WPF=20=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MaaWpfGui/Main/AsstProxy.cs | 39 ++++++++++------------- src/MaaWpfGui/Services/Web/HttpService.cs | 7 ++-- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/src/MaaWpfGui/Main/AsstProxy.cs b/src/MaaWpfGui/Main/AsstProxy.cs index ac39296fed..3e004af57d 100644 --- a/src/MaaWpfGui/Main/AsstProxy.cs +++ b/src/MaaWpfGui/Main/AsstProxy.cs @@ -604,12 +604,7 @@ namespace MaaWpfGui.Main } } - bool buy_wine = false; - if (_latestTaskId.ContainsKey(TaskType.Mall) && Instances.SettingsViewModel.DidYouBuyWine()) - { - buy_wine = true; - } - + bool buy_wine = _latestTaskId.ContainsKey(TaskType.Mall) && Instances.SettingsViewModel.DidYouBuyWine(); _latestTaskId.Clear(); Instances.TaskQueueViewModel.ResetFightVariables(); @@ -1253,29 +1248,27 @@ namespace MaaWpfGui.Main string host = address_[0].Equals("emulator") ? "127.0.0.1" : address_[0]; int port = int.Parse(address_[1]); - using (var client = new TcpClient()) + using var client = new TcpClient(); + try { - try - { - IAsyncResult result = client.BeginConnect(host, port, null, null); - bool success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(.5)); + IAsyncResult result = client.BeginConnect(host, port, null, null); + bool success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(.5)); - if (success) - { - client.EndConnect(result); - return true; - } - else - { - client.Close(); - return false; - } - } - catch (Exception) + if (success) { + client.EndConnect(result); + return true; + } + else + { + client.Close(); return false; } } + catch (Exception) + { + return false; + } } /// diff --git a/src/MaaWpfGui/Services/Web/HttpService.cs b/src/MaaWpfGui/Services/Web/HttpService.cs index e8adae7b66..1ebd9d0330 100644 --- a/src/MaaWpfGui/Services/Web/HttpService.cs +++ b/src/MaaWpfGui/Services/Web/HttpService.cs @@ -108,7 +108,7 @@ namespace MaaWpfGui.Services.Web { var response = await GetAsync(uri, extraHeader); - if (response == null || response.StatusCode != HttpStatusCode.OK) + if (response?.StatusCode != HttpStatusCode.OK) { return null; } @@ -120,7 +120,7 @@ namespace MaaWpfGui.Services.Web { var response = await GetAsync(uri, extraHeader); - if (response == null || response.StatusCode != HttpStatusCode.OK) + if (response?.StatusCode != HttpStatusCode.OK) { return null; } @@ -183,7 +183,7 @@ namespace MaaWpfGui.Services.Web var response = await GetAsync(uri, extraHeader: new Dictionary { { "Accept", contentType } }, httpCompletionOption: HttpCompletionOption.ResponseHeadersRead); - if (response == null || response.StatusCode != HttpStatusCode.OK) + if (response?.StatusCode != HttpStatusCode.OK) { return false; } @@ -264,7 +264,6 @@ namespace MaaWpfGui.Services.Web } HttpClient client = new HttpClient(handler); - client = new HttpClient(handler); client.DefaultRequestHeaders.Add("User-Agent", UserAgent); client.Timeout = TimeSpan.FromSeconds(15); return client;