user4340666
user4340666

Reputation: 1473

Ping multiple IP Addresses in parallel - C#

I created an application to ping multiple specific IP Addresses in the Network at the same time , display a green PictureBox if the ping succeded and red PictureBox if failed. For that i used this code:

private void button1_Click(object sender, EventArgs e)
{
    DataTable pingResults = new DataTable();
    pingResults.Columns.AddRange(new DataColumn[] {
                new DataColumn("IP", typeof(string)),
                new DataColumn("Message",typeof(string))});
    try
    {
        pingResults.Clear();
        List<string> ipAddress = new List<string>();
        ipAddress.Add("10.100.1.1");
        ipAddress.Add("10.100.1.2");
        ipAddress.Add("10.100.1.100");

        List<PictureBox> pictureBoxList = new List<PictureBox>();

        for (int i = 1; i < 4; i++)
        {
            pictureBoxList.Add((PictureBox)Controls.Find("pictureBox" + i, true)[0]);
        }

        for (int i = 0; i < ipAddress.Count; i++)
        {
            Ping ping = new Ping();
            PingReply pingReply = ping.Send(ipAddress[i].ToString());

            pictureBoxList[i].BackColor = (pingReply.Status == IPStatus.Success) ? Color.Green : Color.Red;

            Thread.Sleep(1000);
        }


    }
    catch (Exception ex)
    {

    }
}

This code works but it takes too long time. I tried a lot of examples here at stackoverflow using BackgroundWorker but i didn't get how to do it

Upvotes: 1

Views: 3743

Answers (2)

Peter-Yu
Peter-Yu

Reputation: 191

You could try Parallel.For, it's simple.

Parallel.For(0, ipAddress.Count() - 1, (i, loopState) =>
{
  Ping ping = new Ping();
  PingReply pingReply = ping.Send(ipAddress[i].ToString());
  //for wpf
  Dispatcher.BeginInvoke((Action)delegate()
  {    
    pictureBoxList[i].BackColor = (pingReply.Status == IPStatus.Success) ? Color.Green : Color.Red;    
  });
  //for winform
  SetColor(pictureBoxList[i], pingReply.Status == IPStatus.Success);
});
//for winform delegate
delegate void SafeSetColor(PictureBox pb, bool success);
private void SetColor(PictureBox pb, bool success)
{
 if(pb.InvokeRequired)
 {
   SafeSetColor objSet=new SafeSetColor(SetColor);
   pb.Invoke(objSet,new object[]{pb, success});
 }
 else
 {
   pb.BackColor = (success) ? Color.Green : Color.Red;
 }
}

Upvotes: 1

user4340666
user4340666

Reputation: 1473

I found a solution, i used the backgroundworker:

DataTable pingResults = new DataTable();

List<string> ipAddress = new List<string>();

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        pingResults.Clear();
        ipAddress.Add("10.100.1.1");
        ipAddress.Add("10.100.1.2");
        ipAddress.Add("10.100.1.3");
        ipAddress.Add("10.100.1.4");
        ipAddress.Add("10.100.1.5");
        ipAddress.Add("10.100.1.100");

        List<PictureBox> pictureBoxList = new List<PictureBox>();

        for (int i = 1; i < 7; i++)
        {
            pictureBoxList.Add((PictureBox)Controls.Find("pictureBox" + i, true)[0]);
        }

        Parallel.For(0, ipAddress.Count(), (i, loopState) =>
        {
            Ping ping = new Ping();
            PingReply pingReply = ping.Send(ipAddress[i].ToString());

            this.BeginInvoke((Action)delegate()
            {
                pictureBoxList[i].BackColor = (pingReply.Status == IPStatus.Success) ? Color.Green : Color.Red;
            });

        });
    }

private void button1_Click(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
    }

Upvotes: 4

Related Questions