예제 #1
0
        /// <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);
        }
예제 #2
0
        /// <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);
        }
예제 #3
0
        /// <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);
        }
예제 #4
0
        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;
        }
예제 #5
0
        /// <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);
        }
예제 #6
0
        /// <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);
        }
예제 #7
0
 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;
 }
예제 #8
0
 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;
 }
예제 #9
0
        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);
                }
            }
        }
예제 #10
0
        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);
        }
예제 #11
0
 public Work(DoWorkHandler work)
     : this(work, string.Empty, ThreadPriority.Normal, null)
 {
 }
예제 #12
0
        public IWork Add(DoWorkHandler work, QueuePriority queuePriority)
        {
            IWork w = new Work(work, _startInfo.DefaultThreadPriority);

            return(Add(w, queuePriority) ? w : null);
        }
예제 #13
0
 /// <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;
 }
예제 #14
0
 public Work(DoWorkHandler work, string description, ThreadPriority threadPriority)
     : this(work, description, threadPriority, null) { }
예제 #15
0
 public Work(DoWorkHandler work, ThreadPriority threadPriority, WorkEventHandler workCompletedHandler)
     : this(work, string.Empty, threadPriority, workCompletedHandler) { }
예제 #16
0
 public Work(DoWorkHandler work, ThreadPriority threadPriority, WorkEventHandler workCompletedHandler)
     : this(work, string.Empty, threadPriority, workCompletedHandler)
 {
 }
예제 #17
0
    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);
    }
예제 #18
0
 public IntervalWork(DoWorkHandler work, TimeSpan interval)
 {
     WorkLoad  = work;
     _interval = interval;
 }
예제 #19
0
 /// <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;
 }
예제 #20
0
 public IWork Add(DoWorkHandler work, string description, QueuePriority queuePriority)
 {
   IWork w = new Work(work, description, _startInfo.DefaultThreadPriority);
   return Add(w, queuePriority) ? w : null;
 }
예제 #21
0
        public IWork Add(DoWorkHandler work, string description)
        {
            IWork w = new Work(work, description, _startInfo.DefaultThreadPriority);

            return(Add(w) ? w : null);
        }
예제 #22
0
 public IWork Add(DoWorkHandler work, WorkEventHandler workCompletedHandler)
 {
   IWork w = new Work(work, workCompletedHandler);
   return Add(w) ? w : null;
 }
예제 #23
0
 public IWork Add(DoWorkHandler work, ThreadPriority threadPriority)
 {
   IWork w = new Work(work, threadPriority);
   return Add(w) ? w : null;
 }
예제 #24
0
 public IWork Add(DoWorkHandler work)
 {
   IWork w = new Work(work, _startInfo.DefaultThreadPriority);
   return Add(w) ? w : null;
 }
예제 #25
0
 public Work(DoWorkHandler work, ThreadPriority threadPriority)
     : this(work, string.Empty, threadPriority, null)
 {
 }
예제 #26
0
 public Work(DoWorkHandler work)
     : this(work, string.Empty, ThreadPriority.Normal, null) { }
예제 #27
0
 public Work(DoWorkHandler work, string description, ThreadPriority threadPriority)
     : this(work, description, threadPriority, null)
 {
 }
예제 #28
0
 public Work(DoWorkHandler work, ThreadPriority threadPriority)
     : this(work, string.Empty, threadPriority, null) { }
예제 #29
0
 public IntervalWork(DoWorkHandler work, TimeSpan interval)
 {
   WorkLoad = work;
   _interval = interval;
 }
예제 #30
0
        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);
        }
예제 #31
0
 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;
 }
예제 #32
0
 /// <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;
 }