Ejemplo n.º 1
0
 /// <summary>
 /// Add thread to queue
 /// </summary>
 /// <param name="thread">Thread</param>
 private void AddThreadToQueue(ParameterizedThread thread)
 {
     lock (queue)
     {
         queue.Enqueue(thread);
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Assign work
 /// </summary>
 /// <param name="index">Index</param>
 private void AssignWork(int index)
 {
     lock (queue)
     {
         if (queue.Count > 0)
         {
             ParameterizedThread thread = queue.Dequeue();
             thread.Start();
             threads[index] = thread;
         }
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Number of threads
 /// </summary>
 /// <param name="numOfThreads">Number of threads (smaller or equal 0 uses the processor count)</param>
 public CountableThreadPool(int numOfThreads = 0)
 {
     threads        = new ParameterizedThread[(numOfThreads > 0) ? numOfThreads : Environment.ProcessorCount];
     observerThread = new Thread(new ParameterizedThreadStart((instance) =>
     {
         if (instance is CountableThreadPool)
         {
             CountableThreadPool that = (CountableThreadPool)instance;
             while (observerRunning)
             {
                 lock (threads)
                 {
                     for (int i = 0; i < threads.Length; i++)
                     {
                         ParameterizedThread thread = threads[i];
                         if (thread == null)
                         {
                             that.AssignWork(i);
                         }
                         else
                         {
                             if (thread.Thread.ThreadState != ThreadState.Running)
                             {
                                 try
                                 {
                                     thread.Thread.Join();
                                 }
                                 catch (Exception e)
                                 {
                                     Console.Error.WriteLine(e.Message);
                                 }
                                 threads[i] = null;
                                 that.AssignWork(i);
                             }
                         }
                     }
                 }
                 Thread.Sleep(observerSleepTime);
             }
         }
     }));
     observerThread.Start(this);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Abort
 /// </summary>
 public void Abort()
 {
     lock (queue)
     {
         queue.Clear();
     }
     lock (threads)
     {
         for (int i = 0; i < threads.Length; i++)
         {
             ParameterizedThread thread = threads[i];
             if (thread != null)
             {
                 thread.Dispose();
                 threads[i] = null;
             }
         }
     }
 }