Handle một lỗi Thread chạy trước khi Control được tạo dẫn đến lỗi

This commit is contained in:
Le Chau 2020-06-26 16:55:14 +07:00
parent 30f5f14ee7
commit 5d7dcd63da

View File

@ -23,26 +23,29 @@ namespace AIParkingApplication
{ {
while (true) while (true)
{ {
lblDateTime.Invoke(new Action(() => if (IsHandleCreated)
{ {
lblDateTime.Text = DateTime.Now.ToString(AppConstant.DATETIME_FORMAT); lblDateTime.Invoke(new Action(() =>
})); {
lblDateTime.Text = DateTime.Now.ToString(AppConstant.DATETIME_FORMAT);
}));
lblPingTimeC3.Invoke(new Action(() => lblPingTimeC3.Invoke(new Action(() =>
{ {
PingResult pingResult = GetPingStatus(c3IP); PingResult pingResult = GetPingStatus(c3IP);
lblPingTimeC3.Text = $"{pingResult.ReplyTime} ms"; lblPingTimeC3.Text = $"{pingResult.ReplyTime} ms";
lblPingTimeC3.BackColor = pingResult.BackColor; lblPingTimeC3.BackColor = pingResult.BackColor;
lblPingTimeC3.ForeColor = pingResult.ForceColor; lblPingTimeC3.ForeColor = pingResult.ForceColor;
})); }));
lblPingTimeServer.Invoke(new Action(() => lblPingTimeServer.Invoke(new Action(() =>
{ {
PingResult pingResult = GetPingStatus(webServerIP); PingResult pingResult = GetPingStatus(webServerIP);
lblPingTimeServer.Text = $"{pingResult.ReplyTime} ms"; lblPingTimeServer.Text = $"{pingResult.ReplyTime} ms";
lblPingTimeServer.BackColor = pingResult.BackColor; lblPingTimeServer.BackColor = pingResult.BackColor;
lblPingTimeServer.ForeColor = pingResult.ForceColor; lblPingTimeServer.ForeColor = pingResult.ForceColor;
})); }));
}
Thread.Sleep(1000); Thread.Sleep(1000);
} }
} }