void OnExecutor_ProcessItemCompleted(ThreadExecutor <T> source, ThreadExecutorItem <T> item) { ModuleProc PROC = new ModuleProc(DYN_MODULE_NAME, "OnExecutor_ProcessItemCompleted"); try { lock (_lockList) { try { Interlocked.Decrement(ref _activeWorkers); this.ActiveWorkerThreadDecrement(source.UniqueKey, item.UniqueKey); } catch (Exception ex) { Log.Exception(PROC, ex); } } // wake the producers who are waiting to produce if (_fullWaiters > 0) { lock (_lockFullEvent) { Monitor.Pulse(_lockFullEvent); } } } catch (Exception ex) { Log.Exception(PROC, ex); } }
protected virtual void DoWorkItem(E executor, T item) { using (ILogMethod method = Log.LogMethod(this.DYN_MODULE_NAME, "DoWorkItem")) { try { if (!item.Equals(default(T)) && executor != null) { try { Interlocked.Increment(ref _activeWorkers); this.ActiveWorkerThreadIncrement(executor.UniqueKey, item.UniqueKey); ThreadExecutorItem <T> executorItem = new ThreadExecutorItem <T>(item); executor.QueueWorkerItem(executorItem); } catch (Exception ex) { method.Exception(ex); } } } catch (Exception ex) { method.Exception(ex); } } }
private void DoWorkItem(ThreadExecutor <T> executor, T item) { ModuleProc PROC = new ModuleProc(DYN_MODULE_NAME, "DoWorkItem"); try { if (!item.Equals(default(T))) { try { if (executor != null) { ThreadExecutorItem <T> executorItem = new ThreadExecutorItem <T>(item); executor.QueueWorkerItem(executorItem); Interlocked.Increment(ref _activeWorkers); this.ActiveWorkerThreadIncrement(executor.UniqueKey, item.UniqueKey); } } catch (Exception ex) { Log.Exception(PROC, ex); } } } catch (Exception ex) { Log.Exception(PROC, ex); } }
private void DoWorkItem(T item) { ModuleProc PROC = new ModuleProc(DYN_MODULE_NAME, "DoWorkItem"); try { if (!item.Equals(default(T))) { try { if (_workersFree.First != null) { ThreadExecutor <T> executor = _workersFree.First.Value; string executorKey = executor.UniqueKey; string itemKey = item.UniqueKey; List <ThreadExecutorItem <T> > workerSequence = null; _workersFree.RemoveFirst(); if (!_workersRunning.ContainsKey(executorKey)) { _workersRunning.Add(executorKey, executor); } if (!_workerSequences.ContainsKey(itemKey)) { _workerSequences.Add(itemKey, new List <ThreadExecutorItem <T> >()); } workerSequence = _workerSequences[itemKey]; EventWaitHandle wh = null; if (workerSequence.Count > 0) { //wh = new ManualResetEvent(false); wh = executor.QueueSignal; wh.Reset(); } ThreadExecutorItem <T> executorItem = new ThreadExecutorItem <T>(item); executorItem.WaitHandle = wh; workerSequence.Add(executorItem); executor.QueueWorkerItem(executorItem); } } catch (Exception ex) { Log.Exception(PROC, ex); } } } catch (Exception ex) { Log.Exception(PROC, ex); } }
public override void QueueWorkerItem(ThreadExecutorItem <T> item) { if (this.ExecutorService.IsShutdown) { return; } if (item != null) { _queue.Enqueue(item); if (_checkItemCount) { Interlocked.Increment(ref _itemCount); } } }
private void OnProcessItemCompleted(ThreadExecutorItem <T> item) { ModuleProc PROC = new ModuleProc(DYN_MODULE_NAME, "OnProcessItemCompleted"); try { if (this.ProcessItemCompleted != null) { this.ProcessItemCompleted(this, item); } } catch (Exception ex) { Log.Exception(PROC, ex); } }
private void OnProcessItemCompleted(ThreadExecutorItem <T> item) { using (ILogMethod method = Log.LogMethod(this.DYN_MODULE_NAME, "OnProcessItem")) { try { if (this.ProcessItemCompleted != null) { this.ProcessItemCompleted(this, item); } } catch (Exception ex) { method.Exception(ex); } } }
public override void QueueWorkerItem(ThreadExecutorItem <T> item) { if (this.ExecutorService.IsShutdown) { return; } using (ILogMethod method = Log.LogMethod(this.DYN_MODULE_NAME, "QueueWorkerItem")) { try { if (item != null) { string uniqueKey = item.UniqueKey; ConcurrentQueue <ThreadExecutorItem <T> > dataQueue = null; if (!_dataQueues.ContainsKey(uniqueKey)) { _dataQueues.Add(uniqueKey, (dataQueue = new ConcurrentQueue <ThreadExecutorItem <T> >())); } else { dataQueue = _dataQueues[uniqueKey]; } dataQueue.Enqueue(item); Interlocked.Increment(ref _itemCount); method.DebugV("( {0}::QueueWorkerItem ) Queue Count : {1:D}, Item Count : {2:D}", _threadName, _dataQueues.Count, _itemCount); _signalQueue.Enqueue(0); } } catch (Exception ex) { method.Exception(ex); } } }
private void DoWork() { ModuleProc PROC = new ModuleProc(DYN_MODULE_NAME, "DoWork"); _doWorkInitialized = InitializeStatus.Completed; int threadId = Thread.CurrentThread.ManagedThreadId; _workerThreadId = threadId; try { while (!this.ExecutorService.WaitForShutdown()) { T item = default(T); try { ThreadExecutorItem <T> executorItem = _queue.Dequeue(); if (this.ExecutorService.IsShutdown) { Log.Info(PROC, string.Format("Shutdown called. Thread : {0:D} is exiting now.", threadId)); break; } if (executorItem == null) { Log.Info(PROC, "Invalid executor item received."); continue; } // actual item Log.DescriptionV(PROC, "Thread : {0:D}, Queue Size : {1:D}", threadId, _queue.QueueCount); item = executorItem.Item; // if any preceeding item there, then wait for it to finish if (executorItem.WaitHandle != null) { // Waiting for previous item to finish WaitHandle wh = executorItem.WaitHandle; Log.Info(PROC, "Waiting for previous item to finish for : " + item.UniqueKey); // if shutdown called? if (this.ExecutorService.WaitForShutdown(wh)) { Log.Info(PROC, string.Format("Shutdown called. Thread : {0:D} is exiting now.", threadId)); break; } // get the signal from the previous item Log.Info(PROC, "Signal got from previous item for : " + item.UniqueKey); } // still have item if (this.ExecutorService.IsShutdown) { Log.Info(PROC, string.Format("Shutdown called. Thread : {0:D} is exiting now.", threadId)); break; } // actual processing and processing completion if (!item.Equals(default(T))) { this.OnProcessItem(item); this.OnProcessItemCompleted(executorItem); if (_checkItemCount && (_itemCount > 0)) { Interlocked.Decrement(ref _itemCount); } } } catch (Exception ex) { Log.Exception(PROC, ex); } } } catch (Exception ex) { Log.Exception(PROC, ex); } finally { Log.Info(PROC, string.Format("Shutdown called. Thread : {0:D} has finished his work.", Thread.CurrentThread.ManagedThreadId)); this.Shutdown(); } }
private void DoWork() { using (ILogMethod method = Log.LogMethod(this.DYN_MODULE_NAME, "DoWork")) { try { _workerThreadId = Thread.CurrentThread.ManagedThreadId; _threadName = Thread.CurrentThread.Name; if (_threadName.IsEmpty()) { _threadName = "Thread_" + Thread.CurrentThread.ManagedThreadId.ToString(); } method.InfoV("( {0}::DoWork ) {1} started on the thread : {2:D}", _threadName, _uniqueKey, _workerThreadId); while (!_executorService2.WaitForShutdown()) { T item = default(T); try { byte signalItem = _signalQueue.Dequeue(); do { _dataFound = 0; _dataProcessed = 0; if (_executorService2.IsShutdown) { method.InfoV("( {0}::DoWork ) Shutdown called. Thread : {1:D} is exiting now.", _threadName, _workerThreadId); break; } int queuesCount = _dataQueues.Count; Parallel.ForEach <KeyValuePair <string, ConcurrentQueue <ThreadExecutorItem <T> > > >(_dataQueues, (KeyValuePair <string, ConcurrentQueue <ThreadExecutorItem <T> > > p, ParallelLoopState ps) => { int taskId = Task.CurrentId.SafeValue(); int queueCount = 0; if (_executorService2.WaitTokenSource.IsCancellationRequested) { ps.Stop(); } ConcurrentQueue <ThreadExecutorItem <T> > queue = p.Value; queueCount = queue.Count; // data found to process if (queueCount > 0) { try { ThreadExecutorItem <T> executorItem = null; if (queue.TryDequeue(out executorItem)) { _dataFound++; _dataProcessed++; method.DebugV("( {0}::DoWork_PLStart::{1} ) Item Count : {2:D}", _threadName, p.Key, queueCount); if (_executorService2.IsShutdown) { method.InfoV("( {0}::DoWork ) Shutdown called. Thread : {1:D} is exiting now.", _threadName, _workerThreadId); ps.Stop(); } if (executorItem == null) { method.InfoV("( {0}::DoWork ) Shutdown called. Invalid executor item received.", _threadName); return; } // actual item method.DebugV("( {0}::DoWork ) Thread : {1:D}, Queue Size : {2:D}", _threadName, _workerThreadId, queue.Count); item = executorItem.Item; // still have item if (_executorService2.IsShutdown) { method.InfoV("( {0}::DoWork ) Shutdown called. Thread : {1:D} is exiting now.", _threadName, _workerThreadId); ps.Stop(); } // actual processing and processing completion if (!item.Equals(default(T))) { this.OnProcessItem(item); this.OnProcessItemCompleted(executorItem); } } } catch (Exception ex) { method.Exception(ex); } finally { method.DebugV("( {0}::DoWork_PLEnd::{1} ) Item Count : {2:D}", _threadName, p.Key, queueCount); } } else { method.DebugV("( {0}::DoWork_PLStartEnd::{1} ) Item Count : {3:D}", _threadName, p.Key, queueCount); } }); if (_dataProcessed > 0 && ((_itemCount - _dataProcessed) >= 0)) { int newCount = (_itemCount - _dataProcessed); Interlocked.Exchange(ref _itemCount, newCount); method.DebugV("( {0}::DoWork_ItemComplete ) Queue Count : {1:D}, Item Count : {2:D}", _threadName, queuesCount, _itemCount); } if (_executorService2.WaitForShutdown()) { break; } } while (_dataFound > 0); } catch (Exception ex) { method.Exception(ex); } } } catch (Exception ex) { method.Exception(ex); } finally { method.InfoV("( {0}::DoWork ) Shutdown called. Thread : {1:D} has finished its work.", _threadName, _workerThreadId); this.Shutdown(); } } }
void OnExecutor_ProcessItemCompleted(ThreadExecutor <T> source, ThreadExecutorItem <T> item) { ModuleProc PROC = new ModuleProc(DYN_MODULE_NAME, "OnExecutor_ProcessItemCompleted"); try { lock (_lockList) { try { string executorKey = source.UniqueKey; string itemKey = item.UniqueKey; if (_workersRunning.ContainsKey(executorKey)) { ThreadExecutor <T> executor = _workersRunning[executorKey]; if (source == executor) { _workersRunning.Remove(executorKey); _workersFree.AddLast(executor); if (_workerSequences.ContainsKey(itemKey)) { List <ThreadExecutorItem <T> > workerSequence = _workerSequences[itemKey]; if (workerSequence.Count > 0) { ThreadExecutorItem <T> sequenceItem = workerSequence[0]; EventWaitHandle wh = sequenceItem.WaitHandle; if (wh != null) { //wh.Close(); //wh = null; wh.Reset(); } workerSequence.RemoveAt(0); if (workerSequence.Count > 0) { wh = workerSequence[0].WaitHandle; if (wh != null) { wh.Set(); } } } } } } } catch (Exception ex) { Log.Exception(PROC, ex); } } // wake the producers who are waiting to produce if (_fullWaiters > 0) { lock (_lockFullEvent) { Monitor.Pulse(_lockFullEvent); } } } catch (Exception ex) { Log.Exception(PROC, ex); } }