/// <summary> /// Called from different thread when an element's work is done. /// </summary> /// <param name="elem"></param> private void Done(IWorkElement elem) { if (closing) { return; } InvokeShowError(elem.Error); string res = elem.Error == null ? elem.Result : string.Empty; var textBox = (elem.ResultContainer as TextBox); textBox.Invoke((Action) delegate { textBox.Text = res; }); }
/// <summary> /// Add item to the input work queue. /// </summary> /// <param name="elem">The element to be added to the queue.</param> public void AddWork(IWorkElement elem) { lock (inputQueue) { if (inputQueue.Count >= MAX_Q_SIZE) { elem.Error = new Exception("Queue is full."); CallBack(elem); return; } inputQueue.Enqueue(elem); Monitor.PulseAll(inputQueue); } }
/// <summary> /// Synchronous command execution. /// This is not used. It blocks the GUI. /// </summary> /// <param name="elem"></param> private void RunCommand(IWorkElement elem) { (elem.ResultContainer as TextBox).Text = string.Empty; ClearError(); elem.Execute(); if (elem.Error != null) { ShowError(elem.Error, showDialog: false); } else { (elem.ResultContainer as TextBox).Text = elem.Result; } }
/// <summary> /// Thread routine (loop) /// </summary> private void Run() { while (!ShouldStop) { //Get first element from queue IWorkElement e = GetWork(); //If null is returned then the app wants to exit. if (e == null) { return; } //Handle the element. Do it's work and call the callback routine when done. busy = true; e.Execute(); CallBack(e); busy = false; } }
/// <summary> /// Get an element out of the input queue. /// Blocks if the queue is empty. /// </summary> /// <returns>First element in the queue or null if the application wants to exit.</returns> private IWorkElement GetWork() { lock (inputQueue) { //Wait until the queue is not empty or the application wants to exit. while (inputQueue.Count == 0 && !ShouldStop) { Monitor.Wait(inputQueue); } //If the queue is not empty then return it's first element. if (inputQueue.Count > 0) { IWorkElement e = inputQueue.Dequeue(); return(e); } //The queue is empty but we were signaled. The app must be wanting to exit. Debug.Assert(ShouldStop == true); return(null); } }
/// <summary> /// Async command execution. /// </summary> /// <param name="elem"></param> private void RunCommandAsync(IWorkElement elem) { (elem.ResultContainer as TextBox).Text = string.Empty; ClearError(); worker.AddWork(elem); }