C# SharpPcap Operação cross-thread inválida
Pergunta
Olá, estou tentando converter a captura de pacotes no Exemplo3 na nova versão do SharpPcap SharpPcap-2.2.0rc1.src do aplicativo de console para o aplicativo Windows Forms.
Estou enfrentando um problema quando tentei adicionar pacotes que foram capturados ao controle ListView. Recebo um erro que é:
(Operação entre threads não é válida:Controle 'listViewPackets' acessado de um thread diferente daquele em que foi criado.)
nesta linha:
listViewPackets.Items.Add(e.Packet.ToString());
algum conselho para resolver este problema???
aqui está o meu código:
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 < 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);
/// <summary>
/// Prints the time and length of each received packet
/// </summary>
///
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());
}
}
}
.....................................................................aqui está o código original:
using System;
using System.Collections.Generic;
using SharpPcap;
namespace SharpPcap.Test.Example3
{
/// <summary>
/// Basic capture example
/// </summary>
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 < 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();
}
/// <summary>
/// Prints the time and length of each received packet
/// </summary>
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());
}
}
}
Solução
Ao fazer uma chamada para um controle de outro thread:
if (listView1.InvokeRequired)
{
listView1.BeginInvoke(new MethodInvoker(
() => /*whatever you want with listview */));
}
else
{
/* whatever you want with listview */
}
Se você tem certeza de que sempre estará em outro thread, esqueça o if/else e use o invoke.
EDITAR:
então no seu caso ficaria assim:
if(listView1.InvokeRequired)
{
listView1.BeginInvoke(new MethodInvoker(
() => listViewPackets.Items.Add(e.Packet.ToString()) ));
}
else
{
listViewPackets.Items.Add(e.Packet.ToString());
}
(novamente, ou apenas a chamada BeginInvoke, se sempre for executada em um thread diferente)
Editar 2 Você notará que Shane usa Invoke e eu uso o BegininVoke.Eu uso isso como uma força do hábito.O uso do Invoke bloqueará o thread da interface do usuário e, se você estiver executando uma operação que demora mais, o uso do BeginInvoke executará a atualização na interface do usuário de forma assíncrona.
Outras dicas
Você precisa usar Invocar porque o pacote está chegando em um thread diferente.Os controles da UI não podem ser modificados em um thread diferente daquele em que foram criados.Invoke executará o delegado fornecido no thread da UI.Por exemplo, você poderia fazer isso:
this.Invoke(new MethodInvoker(() => listViewPackets.Items.Add(e.Packet.ToString())), null);