Exemplo n.º 1
0
 public void AddHandler(WebThreadHandler handler, object state)
 {
     lock (this._thread_lock) {
         if (0 >= this._available_thread_indexes.Count)
         {
             Trace.TraceInformation("Enqueuing worker until thread available...");
             this._waiting_queue.Enqueue(new WebThreadQueueItem(handler, state));
         }
         else
         {
             int first_available = this._available_thread_indexes.Pop();
             this._threads[first_available] = new Thread(() => {
                 // Run the handler and put back this index when we're done.
                 handler(state);
                 this._available_thread_indexes.Push(first_available);
                 if (0 < this._waiting_queue.Count && 0 < this._available_thread_indexes.Count)
                 {
                     // Try to re-add the latest item in the queue, letting it take our current spot.
                     this.AddHandler(this._waiting_queue.Dequeue());
                 }
             });
             this._threads[first_available].Name     = this._name + "-" + first_available;
             this._threads[first_available].Priority = this._priority;
             Trace.TraceInformation("Running worker in thread #" + first_available.ToString() + "...");
             this._threads[first_available].Start();
         }
     }
 }
Exemplo n.º 2
0
 public WebThreadQueueItem(WebThreadHandler cb, object s)
 {
     this.Callback = cb;
     this.State    = s;
 }