/// <summary> /// feeding data into queue /// </summary> /// <param name="eventType"></param> /// <param name="data"></param> /// <returns></returns> protected bool FeedData(FeedingEventType eventType, TaskQueueObject data) { bool result = false; if (_queue == null) { OnPythonEvent(null, "FeedData Queue is null"); } else { try { if (!_queue.TryAdd(data, 50)) { OnPythonEvent(null, "FeedData TryAdd Queue Fail"); result = false; } else { result = true; } } catch (InvalidOperationException e) //add fail { OnPythonEvent(null, "FeedData TryAdd Queue InvalidOperationException:" + e.StackTrace); } catch (Exception e) { OnPythonEvent(null, "FeedData Exception:" + e.StackTrace); } } return(result); }
/// <summary> /// task handler function /// </summary> /// <param name="obj"></param> protected void DispatchData(TaskQueueObject obj) { if (obj == null) { return; } if (FeedingEventType.OnSingalReceived.Equals(obj.EventType) && OnSingalReceived != null) { OnSingalReceived(obj.Data); } else if (FeedingEventType.OnTimer1s.Equals(obj.EventType) && OnTimer1s != null) { OnTimer1s(); } else if (FeedingEventType.OnTimer10s.Equals(obj.EventType) && OnTimer10s != null) { OnTimer10s(); } else if (FeedingEventType.OnTimer60s.Equals(obj.EventType) && OnTimer60s != null) { OnTimer60s(); } //unhandle }
/// <summary> /// thread function /// </summary> /// <param name="meta"></param> protected void Run(TaskMeta meta) { if (meta == null || meta._queue == null) { OnPythonEvent(null, "Task Meta or Queue is null, could not start Task"); return; } while (!meta.isTaskStop()) { try { if (!meta._queue.IsCompleted) { TaskQueueObject obj = null; if (!meta._queue.TryTake(out obj, 50)) { //queue is empty } else { DispatchData(obj); } _lastQueuedCount = meta._queue.Count; } else { OnPythonEvent(null, "Queue already completed"); break; } } catch (InvalidOperationException e) ///operating queue exception { OnPythonEvent(null, "Task TryTake Queue InvalidOperationException:" + e.StackTrace); } catch (Exception e) { OnPythonEvent(null, "Task Exception:" + e.StackTrace); } finally { if (meta != null && meta._queue != null) { _lastQueuedCount = meta._queue.Count; } } } OnPythonEvent(null, "Task done"); }