/// <summary> /// Signals the worker thread to start a task /// </summary> /// <param name="task">The task the worker should start on</param> private void startTask(IterTask task) { lock (syncRoot) { this.task = task; } starter.Set(); }
private void workProc() { try{ bool done = false; while (!done) { starter.WaitOne(); //wait for the main thread to signal that its time to start lock (syncRoot) { #if DEBUG if (getStuck) //this gets the thread caught in an infinite loop { int k = 0; int q = 35 / k; while (k < 500) { k = (k + 1) & 0xFF; } } #endif if (task == IterTask.Reset) { reset_iterator(iterHandle); } else if (task == IterTask.Iterate) { iterate_batch(iterHandle); } else if (task == IterTask.Shutdown) { done = true; } task = IterTask.None; } finisher.Set(); //tell the main thread that the task is done } } catch (Exception ex) { lock (syncRoot){ workThreadException = ex; } finisher.Set(); //tell the main thread the task isn't running anymore } }
public Iterator(int id) { this.id = id; this.rand = new Random(); workThread = new Thread(workProc); syncRoot = new object(); starter = new AutoResetEvent(false); finisher = new AutoResetEvent(false); task = IterTask.None; iterHandle = create_iterator(id, (uint)rand.Next()); workThread.IsBackground = true; workThread.Name = "workThread#" + id.ToString(); workThread.Start(); }