0

我正在创建一个带有引擎和 asp.net 客户端界面的应用程序。目前我正在监视服务器是否可以通过 ping 访问。在每个滴答声中,计时器都会调用一些类并为 _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();
    }

因为我调用了一个Open and 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;
    }

evm计算机状态:

    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;
    }
4

1 回答 1

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;
    }
于 2013-08-14T13:33:19.390 回答