SCCManager/Remontor/Pinger/PingManager.cs

112 lines
3.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Remontor.Pinger
{
internal static class PingManager
{
static Dictionary<string, PingResult> ResultPing = new Dictionary<string, PingResult>();
static List<PingTask> TaskList = new List<PingTask>();
static int LifeTimePing = 20;
static Thread PingerHost = new Thread(PingHost);
static public void NewTaskPing(Panel panel, IComp comp)
{
if (ResultPing.ContainsKey(comp.GetNetName()))
{
if ((DateTime.Now - ResultPing[comp.GetNetName()].timePing).Seconds > LifeTimePing)
{
// panel.Visible = true;
// panel.BackColor = Color.Gold;
ResultPing.Remove(comp.GetNetName());
PingTask pingTask = new PingTask(panel, comp);
TaskList.Add(pingTask);
}
else
{
//panel.Visible = true;
if (ResultPing[comp.GetNetName()].Succes)
{
panel.BackColor = Color.Lime;
}
else
{
panel.BackColor = Color.OrangeRed;
}
}
}
else
{
//panel.Visible = true;
// panel.BackColor = Color.Gold;
PingTask pingTask = new PingTask(panel, comp);
TaskList.Add(pingTask);
//if(PingerHost.ThreadState == ThreadState.Unstarted)
//{
// PingerHost.Start();
//}else if(PingerHost.ThreadState == ThreadState.Stopped)
//{
// PingerHost = new Thread(PingHost);
// PingerHost.Start();
//}
}
}
static public void PingHost()
{
// while ()
// {
Thread.Sleep(300);
PingTask[] TaskListBuff = new PingTask[TaskList.Count];
TaskList.CopyTo(TaskListBuff);
TaskList.Clear();
System.Console.WriteLine(TaskListBuff.Length.ToString());
foreach (PingTask Task in TaskListBuff)
{
// Task.PingHost();
}
if (TaskList.Count > 0) PingHost();
else
{
}
// }
}
static public void StopPing()
{
// PingerHost.Join();
}
static public void WriteResult(PingResult pingResult)
{
try
{
ResultPing.Add(pingResult.NameOrAddress, pingResult);
}
catch
{
}
}
}
}