我正在使用引擎和asp.net客户端界面创建应用程序。目前我正在监控服务器是否可通过ping访问。在每个tick上,定时器调用一些类并为_evmComputerEntries中的每个服务器执行IsAlive(ping)
我有一个计时器(测试和故障排除的间隔为5秒)
Timer timer = new Timer(5000);
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
public void timer_Elapsed(object sender, ElapsedEventArgs e)
{
_BLComputerStatus.Open();
foreach (evmComputerEntry evmComputerEntry in _evmComputerEntries)
{
//eventLogEVMON.WriteEntry(DateTime.Now.ToString());
evmComputerStatus evmComputerStatus;
if (IsAlive(evmComputerEntry.ComputerName))
{
evmComputerStatus = new evmComputerStatus(0, evmComputerEntry.ComputerEntryId, 1, DateTime.Now);
}
else
{
evmComputerStatus = new evmComputerStatus(0, evmComputerEntry.ComputerEntryId, 0, DateTime.Now);
}
_BLComputerStatus.InsertComputerStatusEntry(evmComputerStatus);
}
_BLComputerStatus.Close();
}
Becaus我调用Open和Close函数来打开和关闭SQL连接,我认为不应该有问题吗?
这是IsAlive功能:
public bool IsAlive(string server)
{
bool result = false;
try
{
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
PingReply reply = pingSender.Send(server, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
result = true;
}
catch
{
result = false;
}
return result;
}
evmComputerStatus:
private int computerStatusId, status;
private string computerEntryId;
private DateTime statusTime;
public evmComputerStatus(int ComputerStatusId, string ComputerEntryId, int Status, DateTime StatusTime)
{
this.computerStatusId = ComputerStatusId;
this.computerEntryId = ComputerEntryId;
this.status = Status;
this.statusTime = StatusTime;
}
答案 0 :(得分:0)
你应该处理Ping
public bool IsAlive(string server)
{
bool result = false;
try
{
using (Ping pingSender = new Ping()) {
PingOptions options = new PingOptions();
options.DontFragment = true;
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
PingReply reply = pingSender.Send(server, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
result = true;
}
}
catch
{
result = false;
}
return result;
}