void bg_thread_proc(object param) { Thread.CurrentThread.IsBackground = true; long last_queue_proc_tick = 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.queue.Count >= 1) { BatchQueueItem <T> item = this.queue.Dequeue(); current.Add(item); } } if (current.Count >= 1) { do_process_list(current); last_queue_proc_tick = Time.Tick64; } if (this.queue.Count >= 1) { continue; } long now = Time.Tick64; long remain_time = last_queue_proc_tick + (long)this.IdleThreadRemainTimeMsecs - now; if (remain_time >= 1) { new_event_signal.WaitOne((int)remain_time); } else { lock (lockobj) { if (this.queue.Count >= 1) { continue; } thread_mode = 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.queue.Enqueue(q); if (thread_mode == 0) { thread_mode = 1; ThreadObj t = new ThreadObj(bg_thread_proc); } } new_event_signal.Set(); return(q); }