title: C# SharpPcap Cross-thread operation not valid
body: <p>Hi I'm trying to convert packet capturing in Example3 in  SharpPcap new version SharpPcap-2.2.0rc1.src from Console Application to Windows Forms Application.</p>
  
  <p>I'm facing a problem when I tried to add packets has been captured to ListView control I will get an error which is:  </p>
  
  <blockquote>
    <p>(Cross-thread operation not valid: Control 'listViewPackets' accessed from a thread other than the thread it was created on.)  </p>
  </blockquote>
  
  <p>at this line:<br>
   <code>listViewPackets.Items.Add(e.Packet.ToString());</code></p>
  
  <p>any Advice to solve this problem???</p>
  
  <p>here is my code:</p>
  
  <pre><code>using SharpPcap;
  
  namespace Packets
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
  
  
  
              // Retrieve the device list
  
          private void btnLiDevicest_Click(object sender, EventArgs e)
          {
              var devices = LivePcapDeviceList.Instance;
              // If no devices were found print an error
              if (devices.Count &lt; 1)
              {
                  MessageBox.Show("No devices were found on this machine");
                  return;
              }
  
  
              int i = 0;
  
              // Print out the devices
              foreach (LivePcapDevice dev in devices)
              {
  
                  ///* Description */
                  //Console.WriteLine("{0}) {1} {2}", i, dev.Name, dev.Description);
                  cmbListDevice.Items.Add(dev.Name + "   " + dev.Description);
                  i++;
              }
             LivePcapDevice device = devices[1];
              // Register our handler function to the 'packet arrival' event
              device.OnPacketArrival += new PacketArrivalEventHandler(device_OnPacketArrival);
  
              // Open the device for capturing
              int readTimeoutMilliseconds = 1000;
              device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);
              device.StartCapture();
  
  
          }
  
  
              //Console.WriteLine();
              //Console.WriteLine("-- Listening on {0}, hit 'Enter' to stop...",
              //    device.Description);
                      /// &lt;summary&gt;
          /// Prints the time and length of each received packet
          /// &lt;/summary&gt;
          /// 
  
          protected  void device_OnPacketArrival(object sender, CaptureEventArgs e)
          {
              DateTime time = e.Packet.PcapHeader.Date;
              uint len = e.Packet.PcapHeader.PacketLength;
              //Console.WriteLine("{0}:{1}:{2},{3} Len={4}", 
              //    time.Hour, time.Minute, time.Second, time.Millisecond, len);
  
             // Console.WriteLine(e.Packet.ToString());
  
              listViewPackets.Items.Add(e.Packet.ToString());
  
  
          }
  
  
  
          }
  
  
  }
  </code></pre>
  
  <p>.................................
  ....................................
  here is the original code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using SharpPcap;
  namespace SharpPcap.Test.Example3
  {
      /// &lt;summary&gt;
      /// Basic capture example
      /// &lt;/summary&gt;
      public class BasicCap
      {
          public static void Main(string[] args)
          {
              // Print SharpPcap version
              string ver = SharpPcap.Version.VersionString;
              Console.WriteLine("SharpPcap {0}, Example3.BasicCap.cs", ver);
  
              // Retrieve the device list
              var devices = LivePcapDeviceList.Instance;
  
              // If no devices were found print an error
              if(devices.Count &lt; 1)
              {
                  Console.WriteLine("No devices were found on this machine");
                  return;
              }
  
              Console.WriteLine();
              Console.WriteLine("The following devices are available on this machine:");
              Console.WriteLine("----------------------------------------------------");
              Console.WriteLine();
  
              int i = 0;
  
              // Print out the devices
              foreach(LivePcapDevice dev in devices)
              {
                  /* Description */
                  Console.WriteLine("{0}) {1} {2}", i, dev.Name, dev.Description);
                  i++;
              }
  
              Console.WriteLine();
              Console.Write("-- Please choose a device to capture: ");
              i = int.Parse( Console.ReadLine() );
  
              LivePcapDevice device = devices[i];
  
              // Register our handler function to the 'packet arrival' event
              device.OnPacketArrival += 
                  new PacketArrivalEventHandler( device_OnPacketArrival );
  
              // Open the device for capturing
              int readTimeoutMilliseconds = 1000;
              device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);
  
              Console.WriteLine();
              Console.WriteLine("-- Listening on {0}, hit 'Enter' to stop...",
                  device.Description);
  
              // Start the capturing process
              device.StartCapture();
  
              // Wait for 'Enter' from the user.
              Console.ReadLine();
  
              // Stop the capturing process
              device.StopCapture();
  
              Console.WriteLine("-- Capture stopped.");
  
              // Print out the device statistics
              Console.WriteLine(device.Statistics().ToString());
  
              // Close the pcap device
              device.Close();
          }
  
          /// &lt;summary&gt;
          /// Prints the time and length of each received packet
          /// &lt;/summary&gt;
          private static void device_OnPacketArrival(object sender, CaptureEventArgs e)
          {
              DateTime time = e.Packet.PcapHeader.Date;
              uint len = e.Packet.PcapHeader.PacketLength;
              Console.WriteLine("{0}:{1}:{2},{3} Len={4}", 
                  time.Hour, time.Minute, time.Second, time.Millisecond, len);
              Console.WriteLine(e.Packet.ToString());
          }
      }
  }
  </code></pre>