Util - Add OverLoad for UpdateLabel. StatusBar - Refactor.

This commit is contained in:
DucDangAnh 2020-06-30 14:49:32 +07:00
parent 9894ce831d
commit 4ee2507055
2 changed files with 30 additions and 28 deletions

View File

@ -1,19 +1,22 @@
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Windows.Forms;
namespace AIParkingApplication
{
public partial class StatusBar : UserControl
{
private string webServerIP;
private string c3IP;
public StatusBar(string webServerIP, string c3IP)
private string doorAccessControlDeviceIP;
private TimeSpan updateInterval;
public StatusBar(string webServerIP, string doorAccessControlDeviceIP, TimeSpan updateInterval)
{
InitializeComponent();
this.webServerIP = webServerIP;
this.c3IP = c3IP;
this.doorAccessControlDeviceIP = doorAccessControlDeviceIP;
this.updateInterval = updateInterval;
Thread thrStatus = new Thread(new ThreadStart(UpdateStatus));
thrStatus.IsBackground = true;
thrStatus.Start();
@ -25,37 +28,25 @@ namespace AIParkingApplication
{
if (IsHandleCreated)
{
lblDateTime.Invoke(new Action(() =>
{
lblDateTime.Text = DateTime.Now.ToString(AppConstant.DATETIME_FORMAT);
}));
lblDateTime.UpdateLabel(DateTime.Now.ToString(AppConstant.DATETIME_FORMAT));
lblPingTimeC3.Invoke(new Action(() =>
{
PingResult pingResult = GetPingStatus(c3IP);
lblPingTimeC3.Text = $"{pingResult.ReplyTime} ms";
lblPingTimeC3.BackColor = pingResult.BackColor;
lblPingTimeC3.ForeColor = pingResult.ForceColor;
}));
PingResult pingDoorAccessControlResult = GetPingStatus(doorAccessControlDeviceIP);
lblPingTimeC3.UpdateLabel($"{pingDoorAccessControlResult.ReplyTime} ms", pingDoorAccessControlResult.BackColor, pingDoorAccessControlResult.ForeColor);
lblPingTimeServer.Invoke(new Action(() =>
{
PingResult pingResult = GetPingStatus(webServerIP);
lblPingTimeServer.Text = $"{pingResult.ReplyTime} ms";
lblPingTimeServer.BackColor = pingResult.BackColor;
lblPingTimeServer.ForeColor = pingResult.ForceColor;
}));
PingResult pingWebServerResult = GetPingStatus(webServerIP);
lblPingTimeServer.UpdateLabel($"{pingWebServerResult.ReplyTime} ms", pingWebServerResult.BackColor, pingWebServerResult.ForeColor);
}
Thread.Sleep(1000);
Thread.Sleep(updateInterval);
}
}
private PingResult GetPingStatus(string ip)
private PingResult GetPingStatus(string ipAddress)
{
PingResult pingResult = new PingResult();
bool isPingable;
isPingable = Util.IsPingable(ip, out long replyTime);
isPingable = Util.IsPingable(ipAddress, out long replyTime);
if (isPingable)
{
switch (replyTime)
{
case long n when (n >= 0 && n < 1):
@ -71,6 +62,7 @@ namespace AIParkingApplication
pingResult.BackColor = Color.Red;
break;
}
}
else
{
pingResult.ReplyTime = 0;
@ -83,7 +75,7 @@ namespace AIParkingApplication
{
public long ReplyTime { get; set; }
public Color BackColor { get; set; }
public Color ForceColor { get; set; }
public Color ForeColor { get; set; }
}
}
}

View File

@ -86,12 +86,22 @@ namespace AIParkingApplication
}));
}
public static void UpdateLabel(this Label label, string text, Color color)
public static void UpdateLabel(this Label label, string text, Color backColor, Color foreColor)
{
label.Invoke(new Action(() =>
{
label.BackColor = color;
label.Text = text;
label.BackColor = backColor;
label.ForeColor = foreColor;
}));
}
public static void UpdateLabel(this Label label, string text, Color backColor)
{
label.Invoke(new Action(() =>
{
label.Text = text;
label.BackColor = backColor;
}));
}