/// <summary> /// Represents the loop used for performing syncronous work. /// </summary> private async void WorkLoop() { while (_connection.IsOpen) { // receive a work item WorkItem item = null; try { item = await _workQueue.ReceiveAsync(_workCancel.Token).ConfigureAwait(false); if (item == null) { continue; } } catch (TaskCanceledException) { return; } // determine action object o = null; try { o = item.Action(); } catch (Exception ex) { // try and find a task to throw the exception for, if not this is bad if (item.TaskSource != null) { item.TaskSource.SetException(ex); continue; } else { Console.Error.Write(ex.ToString()); } } // check for a task, set a generic result since job handlers should return something // if they have anything to report back if (item.TaskSource != null) { item.TaskSource.SetResult(o); } } // receive all remaining items and cancel while (_workQueue.Count > 0) { WorkItem item = await _workQueue.ReceiveAsync().ConfigureAwait(false); if (item.TaskSource != null) { item.TaskSource.SetCanceled(); } } // mark as completed _workQueue.Complete(); }
private static void ThreadWorker() { while (true) { //Wait for pulse from ThreadPool, signifying a new work item has been queued // ReSharper disable once InconsistentlySynchronizedField ThreadSynch.WaitOne(); var workItem = new WorkItem(); lock (ThreadActions) { //pull the next work item off of the queue if (ThreadActions.Count > 0) { workItem = ThreadActions.Dequeue() as WorkItem; } //no pending actions - reset threads so they wait for next pulse. else { lock (ThreadSynch) ThreadSynch.Reset(); } } //if no action, go back to waiting. if (workItem?.Action == null) { continue; } //workItem.Action(); //FlightComputer.TriggerEvent(workItem.Loggable, ref workItem.PacketData); //if (workItem.Persistent) QueueWorkItem(workItem); //Debug.Print("Current Thread Queue count: " + ThreadActions.Count); //safe try { //try to execute, then trigger any events, then re-add to queue if repeatable. workItem.Action(); FlightComputer.TriggerEvent(workItem.Loggable, ref workItem.PacketData); if (workItem.Persistent) { QueueWorkItem(workItem); } } catch (Exception e) { Rebug.Print("ThreadPool: Unhandled error executing action - " + e.Message + e.InnerException); Rebug.Print("StackTrace: " + e.StackTrace); //maybe just reset the flight computer? } } }
private void ExecuteProc(object state) { var group = (WorkGroup)state; do { if (_stopped) { return; } WorkItem item = null; bool empty; lock (group.Lock) { empty = group.Items.Count == 0; if (!empty) { item = group.Items.Dequeue(); } System.Console.WriteLine("WorkGroup Key is {0}", group.Key); } if (empty) { var linger = DateTime.Now.AddMilliseconds(Linger); while (empty && DateTime.Now < linger) { Thread.Sleep(0); lock (group.Lock) { empty = group.Items.Count == 0; if (!empty) { item = group.Items.Dequeue(); } } } if (empty) { lock (group.Lock) { group.Executing = false; lock (_lock) { if (!group.Key.Equals(DefaultGroup)) { _groups.Remove(group.Key); System.Console.WriteLine("Remove WorkGroup Key is {0}", group.Key); } return; } } } } try { if (item.Action != null) { item.Action(); } else if (item.Callback != null) { item.Callback(item.State); } } catch { // log this somewhere? } } while (true); }