private int ProcessQueuedPackets( )
{
#region For debugging thread problems
#if false
StringBuilder sb = new StringBuilder();
sb.AppendFormat("[0x{0:x}]{1}()\n", Thread.CurrentThread.ManagedThreadId, System.Reflection.MethodInfo.GetCurrentMethod().Name);
foreach (ProcessThread pt in Process.GetCurrentProcess().Threads)
{
sb.AppendFormat("[0x{0:x}] State: {1}\n", pt.Id, pt.ThreadState.ToString());
}
Debug.WriteLine(sb.ToString());
Debug.Flush();
#endif
#endregion
int cnt = 0;
while ( cnt < 1000 )
{
PacketData.PacketWrapper envelope = null;
lock ( PacketQueue )
{
if ( PacketQueue.Count > 0 )
envelope = PacketQueue.Dequeue( );
}
if ( envelope == null )
break;
#pragma warning disable 420 // reference to a volatile field is valid for Interlocked call
Interlocked.Decrement( ref _queueCount );
#pragma warning restore 420
#pragma warning disable 420 // reference to a volatile field is valid for Interlocked call
envelope.PacketNumber = System.Threading.Interlocked.Increment( ref _rawPacketCount ) - 1;
#pragma warning restore 420
// if (PacketArrival != null)
// {
// PacketArrival(this, new PacketArrivalEventArgs(envelope));
// }
SavePacket( envelope );
cnt++;
}
return cnt;
}