views:

60

answers:

3

I want to get alive or dead ip addresses from a big lan. but it takes so many times. I decided to use backgroundworker here is my code:

try
{

    this.Status.Text = "Collecting Information...";

    if(this.TxtWorkGroup.Text.Trim() == "")
    {
        MessageBox.Show("The Work Group name Should Not be Empty");
        return;
    }


    // Use Your work Group WinNT://&&&&(Work Group Name)
    DirectoryEntry DomainEntry = new DirectoryEntry("WinNT://" + this.TxtWorkGroup.Text.Trim());
    DomainEntry.Children.SchemaFilter.Add("computer");


    // To Get all the System names And Display with the Ip Address
    foreach(DirectoryEntry machine in DomainEntry.Children)
    {
        string[] Ipaddr = new string[3];
        Ipaddr[0] = machine.Name;


        System.Net.IPHostEntry Tempaddr = null;

        try
        {
            Tempaddr = (System.Net.IPHostEntry)Dns.GetHostByName(machine.Name);
        }
        catch(Exception)
        {
            //MessageBox.Show("Unable to connect woth the system :" + machine.Name );
                        deadHostList.Items.Add(machine.Name);
            continue;
        }
        System.Net.IPAddress[] TempAd = Tempaddr.AddressList;
        foreach(IPAddress TempA in TempAd)
        {
            Ipaddr[1] = TempA.ToString();

            byte[] ab = new byte[6];
            int len = ab.Length;

            // This Function Used to Get The Physical Address
            int r = SendARP( (int) TempA.Address, 0, ab, ref len );
            string mac = BitConverter.ToString( ab, 0, 6 );

            Ipaddr[2] = mac;
        }           

        System.Windows.Forms.ListViewItem TempItem = new ListViewItem(Ipaddr);

        this.ListHostIP.Items.Add(TempItem);
    }

    this.Status.Text = "Displayed";
}
catch(Exception ex)
{
    MessageBox.Show(ex.Message,"Error",System.Windows.Forms.MessageBoxButtons.OK  );
    Application.Exit();
}

but when I try to use these codes in backgroundWorker1_DoWork event it gives me error messages

Cross-thread operation not valid: Control 'deadHostList' accessed from a thread other than the thread it was created on

How can I modify my codes?

+2  A: 

You cannot and should not access UI controls from any thread other than the thread that created the control. I guess your deadHostList is a ListView control or something similar.

You can marshal a request from the background thread to the UI thread by using Control.Invoke or Control.BeginInvoke

Chris Taylor
A: 

You need to always marshal calls to UI elements, such as your list control, onto the UI thread.

You can do this via Control.Invoke. Change this:

deadHostList.Items.Add(machine.Name);

To:

string name = machine.Name;
deadHostList.Invoke(new Action( () => deadHostList.Items.Add(name)));

You'll also need to do the same thing later for ListHostIP - make sure to use Control.Invoke to wrap that call as well.

Reed Copsey
+2  A: 

As Chris and Reed stated....you can only modify a Ui control from the thread that the control was created on...

You can also use the BackgroundWorker's ProgressChanged event for Ui updates....

  var worker = new BackgroundWorker()
  {
    WorkerReportsProgress = true,
    WorkerSupportsCancellation = true
  };

  /// runs on background thread
  worker.DoWork += (s, e) =>
  {
    while (!done)
    { 
      DoSomeWork();
      // send a message to the Ui
      // via the ProgressChanged event
      worker.ReportProgress(percent, statusMessage); 
    }
  };

  /// the ProgressChanged event runs on the UI thread
  worker.ProgressChanged += (s, e) =>
  {
    var msg = (MyStatusMessage)e.UserState;
    someUiControl.Items.Add(msg.Text);
  };

  /// also runs on Ui thread
  worker.RunWorkerCompleted += (s, e) =>
  {

  };

  worker.RunWorkerAsync();
Rusty
Thanks for your answer. I understand to get my records in worker.ProgressChanged event. I will try thisbut I want to use multi thread because there are mode than 1000 computers in my network. can I do something with backgroundworker?
Rapunzo