void BackgroundThreadProc(object?param) { Thread.CurrentThread.IsBackground = true; long lastQueueProcTick = 0; //Dbg.WhereThread($"BatchQueue<{typeof(T).Name}>: Start background thread."); th.IncrementMe++; while (true) { List <BatchQueueItem <T> > current = new List <BatchQueueItem <T> >(); lock (LockObj) { while (this.ItemQueue.Count >= 1) { BatchQueueItem <T> item = this.ItemQueue.Dequeue(); current.Add(item); } } if (current.Count >= 1) { DoProcessList(current); lastQueueProcTick = Time.Tick64; } if (this.ItemQueue.Count >= 1) { continue; } long now = Time.Tick64; long remainTime = lastQueueProcTick + (long)this.IdleThreadRemainTimeMsecs - now; if (remainTime >= 1) { NewEventSignal.WaitOne((int)remainTime); } else { lock (LockObj) { if (this.ItemQueue.Count >= 1) { continue; } threadMode = 0; //Dbg.WhereThread($"BatchQueue<{typeof(T).Name}>: Stop background thread."); return; } } } }
public BatchQueueItem <T> Add(T item) { BatchQueueItem <T> q = new BatchQueueItem <T>(item); lock (LockObj) { this.ItemQueue.Enqueue(q); if (threadMode == 0) { threadMode = 1; ThreadObj t = new ThreadObj(BackgroundThreadProc); } } NewEventSignal.Set(); return(q); }