SmallRefactor: Đưa HttpClient ra ngoài vòng lặp tránh bị gọi quá nhiều lần

This commit is contained in:
Le Chau 2020-06-25 09:33:57 +07:00
parent a61195ca03
commit 853c49969d

View File

@ -14,6 +14,7 @@ namespace AIParkingApplication
public partial class Statistic : UserControl public partial class Statistic : UserControl
{ {
private const string AIPARKING_CLIENT_HTTP_API_REQUEST = "http://{0}:{1}{2}"; private const string AIPARKING_CLIENT_HTTP_API_REQUEST = "http://{0}:{1}{2}";
private readonly HttpClient client = new HttpClient { BaseAddress = new Uri("http://192.168.1.123:80/"), Timeout = TimeSpan.FromMilliseconds(5000) };
Label lblLoaiXe; Label lblLoaiXe;
Label lblXeMay; Label lblXeMay;
@ -42,8 +43,6 @@ namespace AIParkingApplication
private async void GETDataFromServer() private async void GETDataFromServer()
{ {
try try
{
using (var client = new HttpClient { BaseAddress = new Uri("http://192.168.1.123:80/"), Timeout = TimeSpan.FromMilliseconds(5000) })
{ {
HttpResponseMessage response = await client.GetAsync("/api/statistics"); HttpResponseMessage response = await client.GetAsync("/api/statistics");
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
@ -64,7 +63,7 @@ namespace AIParkingApplication
{ {
lblVehicleTotalOut.Text = vehicle.TotelOut; lblVehicleTotalOut.Text = vehicle.TotelOut;
})); }));
}
} }
catch (Exception ex) catch (Exception ex)
{ {