Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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;
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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;
 }
Exemplo n.º 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;
 }
Exemplo n.º 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);
                }
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 11
0
 public Work(DoWorkHandler work)
     : this(work, string.Empty, ThreadPriority.Normal, null)
 {
 }
Exemplo n.º 12
0
        public IWork Add(DoWorkHandler work, QueuePriority queuePriority)
        {
            IWork w = new Work(work, _startInfo.DefaultThreadPriority);

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

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