public Worker(WorkerPool pool, string id, ThreadPriority priority) { try { #if DEBUG // suppress finalization System.GC.SuppressFinalize(this); #endif _pool = pool; _poolName = pool.Name; _id = id; _wakeThread = ManualResetEventPool.Take(false); _allowDisposal = ManualResetEventPool.Take(false); // start the thread, it should block immediately until a work unit is ready _thread = new Thread(new ThreadStart(WorkerFunc)); _thread.Name = id; _thread.IsBackground = true; _thread.Priority = priority; _thread.Start(); #if DEBUG System.Diagnostics.Debug.WriteLine(DateTime.UtcNow.ToShortTimeString() + ": Creating worker: " + id); // reregister for finalization System.GC.ReRegisterForFinalize(this); #endif } catch { // only dispose if there was an error Dispose(); throw; } }
public void Dispose() { // return the events to the pool ManualResetEventPool.Return(_wakeThread); _wakeThread = null; ManualResetEventPool.Return(_allowDisposal); _allowDisposal = null; #if DEBUG // suppress finalization System.GC.SuppressFinalize(this); #endif }