使用backgroundworker ping多个IP

时间:2014-11-27 12:18:53

标签: c# .net backgroundworker ping

我有一个包含多个IP的数组。

我有一种ping IP的工作方法:

        public static bool PingHost(string nameOrAddress)
    {
        if ( nameOrAddress == null || nameOrAddress == string.Empty)
        {              
            return false;
        }

        bool pingable = false;
        Ping pinger = new Ping();

        try
        {
            PingReply reply = pinger.Send(nameOrAddress);
            pingable = reply.Status == IPStatus.Success;
        }
        catch (PingException ex)
        {
            return false;
        }
        return pingable;
    }

我使用backgroundworker(使用.Net 3.5)来启动ping。完成后我改变了表单的GUI。当我ping一个IP时,一切正常。但我想要做的是运行我所有的IP并在一个IP完成后立即更新表单。所以我必须能够看到第一个IP的结果,而其他人仍然在ping。

private void backgroundWorkerPingHost_DoWork(object sender, DoWorkEventArgs e)
    {
        hostIsPingable = PingHost("www.google.be");
    }

    private void backgroundWorkerPingHost_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Cancelled == true)
        {

        }
        else if (e.Error != null)
        {
            MessageBox.Show("error:" + e.Error.Message);
            HtmlElement htmlDIV = webbSPPagina.Document.GetElementById("isPingable");
            htmlDIV.Style = "width: 20px; height: 20px; background: red; border: solid black 1px;";
        }
        else
        {
            if (hostIsPingable)
            {
                HtmlElement htmlDIV = webbSPPagina.Document.GetElementById("isPingable");
                htmlDIV.Style = "width: 20px; height: 20px; background: green; border: solid black 1px;";
            }
            else
            {
                HtmlElement htmlDIV = webbSPPagina.Document.GetElementById("isPingable");
                htmlDIV.Style = "width: 20px; height: 20px; background: red; border: solid black 1px;";
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

设置backgroundWorkerPingHost.WorkerReportsProgress = True,以便您可以将进度报告回UI线程。

private void backgroundWorkerPingHost_DoWork(object sender, DoWorkEventArgs e)
{
    foreach (...)  // a loop, since you said you're doing this multiple times
    {
        var hostIsPingable = PingHost("www.google.be");

        // Each time you get a response, report the result to the UI thread
        ((BackgroundWorker)sender).ReportProgress(0, hostIsPingable);
    }
}

private void backgroundWorkerPingHost_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // Notified by the DoWork event. Get the result and do something with it
    var hostIsPingable = (bool)e.UserState;

    if (hostIsPingable)
    {
        HtmlElement htmlDIV = webbSPPagina.Document.GetElementById("isPingable");
        htmlDIV.Style = "width: 20px; height: 20px; background: green; border: solid black 1px;";
    }
    else
    {
        HtmlElement htmlDIV = webbSPPagina.Document.GetElementById("isPingable");
        htmlDIV.Style = "width: 20px; height: 20px; background: red; border: solid black 1px;";
    }
}

private void backgroundWorkerPingHost_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Cancelled)
        return;

    // Leave stuff in here that you only want to do once when the worker ends
    if (e.Error != null)
    {
        MessageBox.Show("error:" + e.Error.Message);
        HtmlElement htmlDIV = webbSPPagina.Document.GetElementById("isPingable");
        htmlDIV.Style = "width: 20px; height: 20px; background: red; border: solid black 1px;";
        return;
    }
}