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
{
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 lblXeMay;
@ -43,28 +44,26 @@ namespace AIParkingApplication
{
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");
response.EnsureSuccessStatusCode();
var vehicle = await response.Content.ReadAsAsync<Vehicle>();
lblSoLuongXeMay?.Invoke(new Action(() =>
{
HttpResponseMessage response = await client.GetAsync("/api/statistics");
response.EnsureSuccessStatusCode();
var vehicle = await response.Content.ReadAsAsync<Vehicle>();
lblSoLuongXeMay?.Invoke(new Action(() =>
{
lblSoLuongXeMay.Text = vehicle.NumberOfMoto.ToString();
}));
lblSoLuongOto?.Invoke(new Action(() =>
{
lblSoLuongOto.Text = vehicle.NumberOfCar.ToString();
}));
lblVehicleTotalIn?.Invoke(new Action(() =>
{
lblVehicleTotalIn.Text = vehicle.TotalIn;
}));
lblVehicleTotalOut?.Invoke(new Action(() =>
{
lblVehicleTotalOut.Text = vehicle.TotelOut;
}));
}
lblSoLuongXeMay.Text = vehicle.NumberOfMoto.ToString();
}));
lblSoLuongOto?.Invoke(new Action(() =>
{
lblSoLuongOto.Text = vehicle.NumberOfCar.ToString();
}));
lblVehicleTotalIn?.Invoke(new Action(() =>
{
lblVehicleTotalIn.Text = vehicle.TotalIn;
}));
lblVehicleTotalOut?.Invoke(new Action(() =>
{
lblVehicleTotalOut.Text = vehicle.TotelOut;
}));
}
catch (Exception ex)
{
@ -102,7 +101,7 @@ namespace AIParkingApplication
[JsonProperty("moto")]
public int NumberOfMoto { get; set; }
[JsonProperty("total_in")]
public string TotalIn { get; set; }