Cross-thread calls to WF controls
I am work with SharpPcap library. I have fol开发者_JAVA技巧lowing 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);
}
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 this. And rewrite my code like this:
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);
}
But method PrintIntoGridView not called. What's problem? PS. Sorry for my English.
I guess the device_OnPacketArrival
is already executed on a different thread from the GUI thread, so spawning yet another thread wouldn't be necessary. Try this:
void device_OnPacketArrival(object sender, CaptureEventArgs e)
{
if (InvokeRequired)
{
// If this is executing on a different thread than the GUI thread
var getPacketInfoCallback = new GetPacketInfoCallback(PrintIntoGridView);
Invoke(getPacketInfoCallback, new object[] { e });
}
else
{
// This is the GUI thread =>
// you can safely modify GUI controls
PrintIntoGridView(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
);
}
精彩评论