67 lines
2.1 KiB
C#
67 lines
2.1 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Net.NetworkInformation;
|
|||
|
using System.Threading;
|
|||
|
using SafeMobileLib;
|
|||
|
|
|||
|
namespace MotoTrbo_GW
|
|||
|
{
|
|||
|
class PingThread
|
|||
|
{
|
|||
|
string ip;
|
|||
|
private int interval;
|
|||
|
bool status;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// ping class
|
|||
|
/// </summary>
|
|||
|
/// <param name="ip">dest IP</param>
|
|||
|
/// <param name="interval">interval for pings in seconds</param>
|
|||
|
public PingThread(string ip, int interval)
|
|||
|
{
|
|||
|
this.ip = ip;
|
|||
|
this.interval = interval;
|
|||
|
status = false;
|
|||
|
}
|
|||
|
|
|||
|
public void Worker()
|
|||
|
{
|
|||
|
SafeMobileLib.Utils.WriteLine("PingThread started for "+ip + " interval:"+interval);
|
|||
|
|
|||
|
while (true)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
DateTime time = DateTime.Now;
|
|||
|
//SafeMobileLib.Utils.WriteLine(ip+" start ping");
|
|||
|
Ping pingSender = new Ping();
|
|||
|
PingReply pReply = pingSender.Send(ip);
|
|||
|
if (pReply.Status == IPStatus.Success)
|
|||
|
{
|
|||
|
if (status == false) //going from OFF->ON
|
|||
|
OnNewPingComplete(true, ip);
|
|||
|
status = true;
|
|||
|
}
|
|||
|
else //if (pReply.Status == IPStatus.TimedOut)
|
|||
|
{
|
|||
|
if (status == true) //going from ON->OFF
|
|||
|
OnNewPingComplete(false, ip);
|
|||
|
status = false;
|
|||
|
}
|
|||
|
//SafeMobileLib.Utils.WriteLine(ip + "stop ping" + (time-DateTime.Now)+" status:"+status);
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
SafeMobileLib.Utils.WriteLine("*** Could not ping: " + ip + " *****");
|
|||
|
}
|
|||
|
Thread.Sleep(interval);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public delegate void NewPingCompleteDEl(bool status,string ip);
|
|||
|
public event NewPingCompleteDEl OnNewPingComplete;
|
|||
|
}
|
|||
|
}
|