1 using System; 2 using System.Threading; 3 4 class WorkerThread { 5 public WorkerThread(uint workerNumber, Distributor distributor) { 6 workerNumber_ = workerNumber; 7 distributor_ = distributor; 8 } 9 10 public void doWork() { 11 try { 12 while (doWorkEvent_.WaitOne() && !distributor_.terminateThreads()) { 13 distributor_.doWork(workerNumber_); 14 workIsDoneEvent_.Set(); 15 } 16 workIsDoneEvent_.Set(); 17 } 18 19 catch (Exception x) 20 {Console.WriteLine("Worker thread " + workerNumber_ + x.Message);} 21 } 22 23 public void requestWork() {doWorkEvent_.Set();} 24 25 public AutoResetEvent getWorkIsDoneEvent() {return workIsDoneEvent_;} 26 27 public uint workerNumber_; 28 private Distributor distributor_; 29 private AutoResetEvent doWorkEvent_ = new AutoResetEvent(false); 30 private AutoResetEvent workIsDoneEvent_ = new AutoResetEvent(false); 31 } 32