/// <summary> /// Add work to be performed by the threadpool. /// Can throw ArgumentNullException and InvalidOperationException. /// </summary> /// <param name="work">DoWorkHandler which contains the work to perform</param> /// <param name="description">description for this work</param> /// <param name="queuePriority">QueuePriority for this work</param> /// <returns>IWork reference to work object</returns> public IWork Add(DoWorkHandler work, string description, QueuePriority queuePriority) { IWork w = new Work(work, description, _startInfo.DefaultThreadPriority); Add(w, queuePriority); return(w); }
/// <summary> /// Add work to be performed by the threadpool. /// Can throw ArgumentNullException and InvalidOperationException. /// </summary> /// <param name="work">DoWorkHandler which contains the work to perform</param> /// <param name="description">description for this work</param> /// <param name="queuePriority">QueuePriority for this work</param> /// <param name="threadPriority">System.Threading.ThreadPriority for this work</param> /// <returns>IWork reference to work object</returns> public IWork Add(DoWorkHandler work, string description, QueuePriority queuePriority, ThreadPriority threadPriority) { IWork w = new Work(work, description, threadPriority); Add(w, queuePriority); return(w); }
/// <summary> /// Add work to be performed by the threadpool. /// Can throw ArgumentNullException and InvalidOperationException. /// </summary> /// <param name="work">DoWorkHandler which contains the work to perform</param> /// <param name="workCompletedHandler">WorkEventHandler to be called on completion</param> /// <returns>IWork reference to work object</returns> public IWork Add(DoWorkHandler work, WorkEventHandler workCompletedHandler) { IWork w = new Work(work, workCompletedHandler); Add(w); return(w); }
public override void Process() { // don't perform canceled work if (State == WorkState.CANCELED) { return; } // don't perform work which is in an invalid state if (State != WorkState.INQUEUE) { throw new InvalidOperationException(String.Format("WorkState for work {0} not INQUEUE, but {1}", Description, State)); } State = WorkState.INPROGRESS; // perform work DoWorkHandler workLoad = WorkLoad; if (workLoad != null) { workLoad(); } // mark work as finished and fire work completion delegate State = WorkState.FINISHED; if (WorkCompleted != null) { WorkCompleted(EventArgs); } _running = false; }
/// <summary> /// Add work to be performed by the threadpool. /// Can throw ArgumentNullException and InvalidOperationException. /// </summary> /// <param name="work">DoWorkHandler which contains the work to perform</param> /// <returns>IWork reference to work object</returns> public IWork Add(DoWorkHandler work) { IWork w = new Work(work, _startInfo.DefaultThreadPriority); Add(w); return(w); }
/// <summary> /// Add work to be performed by the threadpool. /// Can throw ArgumentNullException and InvalidOperationException. /// </summary> /// <param name="work">DoWorkHandler which contains the work to perform</param> /// <param name="threadPriority">System.Threading.ThreadPriority for this work</param> /// <returns>IWork reference to work object</returns> public IWork Add(DoWorkHandler work, ThreadPriority threadPriority) { IWork w = new Work(work, threadPriority); Add(w); return(w); }
public Work(DoWorkHandler work, string description, ThreadPriority threadPriority, WorkEventHandler workCompletedHandler) { WorkLoad = work; _description = description; _priority = threadPriority; WorkCompleted = workCompletedHandler; _eventArgs = new WorkEventArgs(this); _simpleWork = true; _state = WorkState.INIT; }
public virtual void Process() { // Don't perform canceled work if (_state == WorkState.CANCELED) { return; } // Don't perform work which is in an invalid state if (_state != WorkState.INQUEUE) { throw new InvalidOperationException(String.Format("WorkState for work '{0}' is {1} but should be {2}", _description, _state, WorkState.INQUEUE)); } // Mark work as in progress if (_simpleWork) { State = WorkState.INPROGRESS; } // Perform work DoWorkHandler workLoad = WorkLoad; if (workLoad != null) { workLoad(); } else { throw new NotImplementedException(); } // Mark work as finished and fire work completion delegate if (_simpleWork) { State = WorkState.FINISHED; if (WorkCompleted != null) { WorkCompleted(_eventArgs); } } }
public void TestWorkQueueLengthBusyCountAndWorkCancellation() { DoWorkHandler workHandler = new DoWorkHandler(delegate() { Thread.Sleep(300); }); Console.Out.WriteLine("----=<TestWorkItemsProcessed>=----"); List <IWork> workList = new List <IWork>(); ThreadPoolStartInfo tpsi = new ThreadPoolStartInfo(1, 1, 10); _pool = new ThreadPool(tpsi); SetupLogging(); _pool.Add(workHandler); Thread.Sleep(10); Assert.AreEqual(1, _pool.BusyThreadCount); for (int i = 0; i < 10; i++) { workList.Add(_pool.Add(workHandler)); } Assert.AreEqual(10, _pool.QueueLength); foreach (IWork work in workList) { work.State = WorkState.CANCELED; } _pool.MinimumThreads = _pool.MaximumThreads = 10; _pool.MinimumThreads = 0; while (_pool.BusyThreadCount > 0) { Thread.Sleep(100); } foreach (IWork work in workList) { Assert.AreEqual(WorkState.CANCELED, work.State); } Assert.AreEqual(0, _pool.QueueLength); Assert.AreEqual(0, _pool.BusyThreadCount); }
public Work(DoWorkHandler work) : this(work, string.Empty, ThreadPriority.Normal, null) { }
public IWork Add(DoWorkHandler work, QueuePriority queuePriority) { IWork w = new Work(work, _startInfo.DefaultThreadPriority); return(Add(w, queuePriority) ? w : null); }
/// <summary> /// Add work to be performed by the threadpool. /// Can throw ArgumentNullException and InvalidOperationException. /// </summary> /// <param name="work">DoWorkHandler which contains the work to perform</param> /// <param name="description">description for this work</param> /// <param name="queuePriority">QueuePriority for this work</param> /// <param name="threadPriority">System.Threading.ThreadPriority for this work</param> /// <returns>IWork reference to work object</returns> public IWork Add(DoWorkHandler work, string description, QueuePriority queuePriority, ThreadPriority threadPriority) { IWork w = new Work(work, description, threadPriority); Add(w, queuePriority); return w; }
public Work(DoWorkHandler work, string description, ThreadPriority threadPriority) : this(work, description, threadPriority, null) { }
public Work(DoWorkHandler work, ThreadPriority threadPriority, WorkEventHandler workCompletedHandler) : this(work, string.Empty, threadPriority, workCompletedHandler) { }
public void TestWorkQueueLengthBusyCountAndWorkCancellation() { DoWorkHandler workHandler = new DoWorkHandler(delegate() { Thread.Sleep(300); }); Console.Out.WriteLine("----=<TestWorkItemsProcessed>=----"); List<IWork> workList = new List<IWork>(); ThreadPoolStartInfo tpsi = new ThreadPoolStartInfo(1, 1, 10); _pool = new ThreadPool(tpsi); SetupLogging(); _pool.Add(workHandler); Thread.Sleep(10); Assert.AreEqual(1, _pool.BusyThreadCount); for (int i = 0; i < 10; i++) { workList.Add(_pool.Add(workHandler)); } Assert.AreEqual(10, _pool.QueueLength); foreach (IWork work in workList) { work.State = WorkState.CANCELED; } _pool.MinimumThreads = _pool.MaximumThreads = 10; _pool.MinimumThreads = 0; while (_pool.BusyThreadCount > 0) { Thread.Sleep(100); } foreach (IWork work in workList) { Assert.AreEqual(WorkState.CANCELED, work.State); } Assert.AreEqual(0, _pool.QueueLength); Assert.AreEqual(0, _pool.BusyThreadCount); }
public IntervalWork(DoWorkHandler work, TimeSpan interval) { WorkLoad = work; _interval = interval; }
/// <summary> /// Add work to be performed by the threadpool. /// Can throw ArgumentNullException and InvalidOperationException. /// </summary> /// <param name="work">DoWorkHandler which contains the work to perform</param> /// <param name="description">description for this work</param> /// <returns>IWork reference to work object</returns> public IWork Add(DoWorkHandler work, string description) { IWork w = new Work(work, description, _startInfo.DefaultThreadPriority); Add(w); return w; }
public IWork Add(DoWorkHandler work, string description, QueuePriority queuePriority) { IWork w = new Work(work, description, _startInfo.DefaultThreadPriority); return Add(w, queuePriority) ? w : null; }
public IWork Add(DoWorkHandler work, string description) { IWork w = new Work(work, description, _startInfo.DefaultThreadPriority); return(Add(w) ? w : null); }
public IWork Add(DoWorkHandler work, WorkEventHandler workCompletedHandler) { IWork w = new Work(work, workCompletedHandler); return Add(w) ? w : null; }
public IWork Add(DoWorkHandler work, ThreadPriority threadPriority) { IWork w = new Work(work, threadPriority); return Add(w) ? w : null; }
public IWork Add(DoWorkHandler work) { IWork w = new Work(work, _startInfo.DefaultThreadPriority); return Add(w) ? w : null; }
public Work(DoWorkHandler work, ThreadPriority threadPriority) : this(work, string.Empty, threadPriority, null) { }
public IWork Add(DoWorkHandler work, string description, QueuePriority queuePriority, ThreadPriority threadPriority, WorkEventHandler workCompletedHandler) { IWork w = new Work(work, description, threadPriority, workCompletedHandler); return(Add(w, queuePriority) ? w : null); }
public IWork Add(DoWorkHandler work, string description, QueuePriority queuePriority, ThreadPriority threadPriority, WorkEventHandler workCompletedHandler) { IWork w = new Work(work, description, threadPriority, workCompletedHandler); return Add(w, queuePriority) ? w : null; }
/// <summary> /// Add work to be performed by the threadpool. /// Can throw ArgumentNullException and InvalidOperationException. /// </summary> /// <param name="work">DoWorkHandler which contains the work to perform</param> /// <param name="queuePriority">QueuePriority for this work</param> /// <returns>IWork reference to work object</returns> public IWork Add(DoWorkHandler work, QueuePriority queuePriority) { IWork w = new Work(work, _startInfo.DefaultThreadPriority); Add(w, queuePriority); return w; }