From f4edbe1021db6fe246e83445a0b666c42c082a56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AE=87=E9=80=B8?= Date: Tue, 5 Jun 2018 23:02:24 +0800 Subject: [PATCH] Reduce HttpClient. --- TsinghuaNet/MainViewModel.vb | 37 +++++++++++++++----------- TsinghuaNet/My Project/AssemblyInfo.vb | 4 +-- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/TsinghuaNet/MainViewModel.vb b/TsinghuaNet/MainViewModel.vb index b442a2b..9b94f0f 100644 --- a/TsinghuaNet/MainViewModel.vb +++ b/TsinghuaNet/MainViewModel.vb @@ -1,5 +1,6 @@ Imports System.Collections.ObjectModel Imports System.Globalization +Imports System.Net.Http Imports Berrysoft.Tsinghua.Net Class MainViewModel @@ -106,13 +107,7 @@ Class MainViewModel Private auth4 As Auth4Helper Private auth6 As Auth6Helper Private net As NetHelper - Private Function InitHelper(Of T As {New, NetHelperBase})(ByRef helper As T) As T - If helper Is Nothing Then - helper = New T() - End If - UpdateHelper(helper) - Return helper - End Function + Private client As New HttpClient() Private Sub UpdateHelper(helper As NetHelperBase) helper.Username = Username helper.Password = Password @@ -121,11 +116,26 @@ Class MainViewModel Get Select Case State Case NetState.Auth4 - Return InitHelper(auth4) + If auth4 Is Nothing Then + auth4 = New Auth4Helper(Username, Password, client) + Else + UpdateHelper(auth4) + End If + Return auth4 Case NetState.Auth6 - Return InitHelper(auth6) + If auth6 Is Nothing Then + auth6 = New Auth6Helper(Username, Password, client) + Else + UpdateHelper(auth6) + End If + Return auth6 Case NetState.Net - Return InitHelper(net) + If net Is Nothing Then + net = New NetHelper(Username, Password, client) + Else + UpdateHelper(net) + End If + Return net Case Else Return Nothing End Select @@ -166,7 +176,7 @@ Class MainViewModel Public ReadOnly Property UseregHelper As UseregHelper Get If usereg Is Nothing Then - usereg = New UseregHelper(Username, Password) + usereg = New UseregHelper(Username, Password, client) Else UpdateHelper(usereg) End If @@ -175,10 +185,7 @@ Class MainViewModel End Property Public Sub DisposeHelpers() - net?.Dispose() - auth4?.Dispose() - auth6?.Dispose() - usereg?.Dispose() + client.Dispose() End Sub End Class diff --git a/TsinghuaNet/My Project/AssemblyInfo.vb b/TsinghuaNet/My Project/AssemblyInfo.vb index f1f41ee..24c914d 100644 --- a/TsinghuaNet/My Project/AssemblyInfo.vb +++ b/TsinghuaNet/My Project/AssemblyInfo.vb @@ -55,5 +55,5 @@ Imports System.Windows ' 方法是按如下所示使用“*”: : ' - - + +