当我在此方法中调用 Pcap.Net 数据包捕获例程时,我应该如何检查 BackgroundWorker DoWork 方法中的 CancellationPending,该例程通过回调进行响应。我能想到的两个选择是:
a) 在 DoWork 方法的底部写一个循环来不断检查 CancellationPending
b) 将检查放入我编写的回调方法中,Pcap.Net 将回调到该方法中 - 但毫无疑问,这里的潜在问题是取消无法正常工作,直到发生另一个补丁匹配并且有回调
建议?
public class MainClass {
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
var worker = sender as BackgroundWorker;
_packetCapturer = new PacketCapturer();
}
}
public class PacketCapturer
public PacketCapturer() {
// Start Capture Here
// Opens PacketCommunicator
// communicator.ReceivePackets(0, PacketCapturerCallback);
}
private static void PacketCapturerCallback(Packet packet) {
// Deal with returned packet
}
}