Hi,
Is my use of ConcurrentQueue here between 2 threads ok? I wanted to check I don't need to "lock" anywhere explicitly. In particular look at the lines where I have in COMMENTS would I drop a packet here...
public class PacketCapturer
{
private static ConcurrentQueue<Packet> _packetQueue = new ConcurrentQueue<Packet>();
public PacketCapturer(IPHostEntry proxyDns, ref BackgroundWorker bw)
{
// start the background thread
var backgroundThread = new System.Threading.Thread(BackgroundThread);
backgroundThread.Start();
// Start Packet Capture with callback via PacketCapturerCallback
}
private void PacketCapturerCallback(Packet packet)
{
_packetQueue.Enqueue(packet);
}
private static void BackgroundThread()
{
while (!BackgroundThreadStop)
{
if (_packetQueue.Count == 0) Thread.Sleep(250);
else
{
ConcurrentQueue<Packet> ourQueue;
ourQueue = _packetQueue; // COULD I DROP A PACKET BETWEEN HERE
_packetQueue = new ConcurrentQueue<Packet>(); // AND HERE???
Console.WriteLine("BackgroundThread: ourQueue.Count is {0}", ourQueue.Count);
}
}
}