public static void Enqueue(WebSocketReqQueueItem item) { lock (s_queue) { s_queue.Enqueue(item); RunClearingThread(); Monitor.Pulse(s_queue); } }
static void ClearingThread(object s) { WebSocketReqQueueItem item = WebSocketReqQueueItem.Empty; while (s_running == 1) { bool foundJob = false; lock (s_queue) { int count = s_queue.Count; if (count > 0) { item = s_queue.Dequeue(); foundJob = true; //run this task } } if (foundJob) { try { item._conn.InvokeReqHandler(item._request); } catch (Exception ex) { } } else { int noJobCount = 0; lock (s_queue) { while (s_queue.Count == 0) { Monitor.Wait(s_queue, 2000); noJobCount++; if (noJobCount > 5) { //auto stop this thread if not job //within a limit time Interlocked.Exchange(ref s_running, 0); Thread mainThread = s_mainClearingThread; mainThread = null; Interlocked.Exchange(ref s_threadCreated, 0); return; } } } } } }