Note that there are some explanatory texts on larger screens.

plurals
  1. POCross-thread calls to WF controls
    primarykey
    data
    text
    <p>I am work with SharpPcap library. I have following code:</p> <pre><code>private void button1_Click(object sender, EventArgs e) { LivePcapDevice device = deviceList[cbDeviceList.SelectedIndex]; device.OnPacketArrival += new PacketArrivalEventHandler(device_OnPacketArrival); device.Open(DeviceMode.Promiscuous, 1000); device.StartCapture(); btStop.Enabled = true; btStartCapture.Enabled = false; } void device_OnPacketArrival(object sender, CaptureEventArgs e) { dgvPacketInfo.Rows.Add(e.Packet.Data, e.Packet.Timeval, e.Packet.LinkLayerType); } </code></pre> <p>I've got an exception is thrown:InvalidOperationException with the message, "Control dgvPacketInfo accessed from a thread other than the thread it was created on." I read <a href="http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&amp;l=EN-US&amp;k=k(EHINVALIDOPERATION.WINFORMS.ILLEGALCROSSTHREADCALL);k(TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV3.5%22);k(DevLang-CSHARP)&amp;rd=true" rel="nofollow noreferrer">this</a>. And rewrite my code like this:</p> <pre><code>private delegate void GetPacketInfoCallback(CaptureEventArgs packet); void device_OnPacketArrival(object sender, CaptureEventArgs e) { Thread newThread = new Thread(delegate() { GetPacketInfoCallback getPacketInfoCallback = new GetPacketInfoCallback(PrintIntoGridView); this.Invoke(getPacketInfoCallback, new object[] {e}); }); } private void PrintIntoGridView(CaptureEventArgs captureEventArgs) { dgvPacketInfo.Rows.Add("1", "2", "3"); // simple for test dgvPacketInfo.Rows.Add(captureEventArgs.Packet.Data, captureEventArgs.Packet.Timeval, captureEventArgs.Packet.LinkLayerType); } </code></pre> <p>But method PrintIntoGridView not called. What's problem? PS. Sorry for my English.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload