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