internal ProducerConsumerQueue(IExecutorService executorService, int capacity, int queueTimeout, bool isLogging) : base(executorService) { _queueTimeout = queueTimeout; _queue = new BlockingBoundQueueUser <T>(executorService, capacity, queueTimeout, isLogging); _dequeueThread = Extensions.CreateThreadAndStart(new ThreadStart(this.OnDequeue), "ProducerConsumerQueue_OnDequeue_"); }
internal AppNotifyServiceClientCallback(IExecutorService executorService, IAppNotifyServiceCallback callback) : base(executorService) { _callback = callback; _queue = new BlockingBoundQueueUser <AppNotifyData>(executorService, -1); Extensions.CreateThreadAndStart(new ThreadStart(this.OnReceiveMessages), "AppNotifyServiceClientCallback_OnReceiveMessages_"); }
private ThreadExecutor <T> AddThreadExecutor(string itemKey) { ModuleProc PROC = new ModuleProc(DYN_MODULE_NAME, "Initialize"); ThreadExecutor <T> executor = null; try { IThreadSafeQueue <ThreadExecutorItem <T> > threadQueue = null; if (_kernelModeQueue) { threadQueue = new BlockingBoundQueueKernel <ThreadExecutorItem <T> >(this.ThreadExecutorService, this.QueueCapacity); } else { threadQueue = new BlockingBoundQueueUser <ThreadExecutorItem <T> >(this.ThreadExecutorService, this.QueueCapacity); } executor = new ThreadExecutor <T>(this.ThreadExecutorService, threadQueue, false, itemKey); executor.ProcessItem += new ExecutorProcessItemHandler <T>(OnExecutor_ProcessItem); executor.ProcessItemCompleted += new ExecutorProcessItemCompletedHandler <T>(OnExecutor_ProcessItemCompleted); _activeWorkersCount.Add(itemKey, new ThreadHashValue() { Executor = executor, ItemCount = 0 }); } catch (Exception ex) { Log.Exception(PROC, ex); } return(executor); }
private void Initialize(string threadSuffix) { using (ILogMethod method = Log.LogMethod(this.DYN_MODULE_NAME, "Initialize")) { string suffix = (threadSuffix.IsEmpty() ? "" : (threadSuffix + "_")); try { _signalQueue = new BlockingBoundQueueUser <byte>(this.ExecutorService, -1); _dataQueues = new StringConcurrentDictionary <ConcurrentQueue <ThreadExecutorItem <T> > >(); _uniqueKey = KEY_PREFIX + Guid.NewGuid().ToString(); this.ExecutorService.AddExecutor(this); if (!Extensions.UseTaskInsteadOfThread) { _thWorker = Extensions.CreateThreadAndStart(this.DoWork, KEY_PREFIX + suffix); } else { _tskWorker = Extensions.CreateLongRunningTask(this.DoWork); } } catch (Exception ex) { method.Exception(ex); } } }
internal AppNotifyServiceClientCallback(IExecutorService executorService, IAppNotifyServiceCallback callback) : base(executorService) { _callback = callback; _queue = new BlockingBoundQueueUser<AppNotifyData>(executorService, -1); Extensions.CreateThreadAndStart(new ThreadStart(this.OnReceiveMessages), "AppNotifyServiceClientCallback_OnReceiveMessages_"); }
internal ThreadExecutor(IExecutorService executorService, IThreadSafeQueue <ThreadExecutorItem <T> > queue, bool checkItemCount, string threadSuffix) : base(executorService) { _queue = queue; _checkItemCount = checkItemCount; this.QueueCapacity = queue.Capacity; this.Initialize(threadSuffix); }
private AppNotifyServiceFactory(IExecutorService executorService, string baseAddress, string path, Type[] knownTypes) : base(executorService) { _queue = new BlockingBoundQueueUser<AppNotifyData>(executorService, -1); Extensions.CreateThreadAndStart(new ThreadStart(this.OnBroadcastMessages), "AppNotifyServiceFactory_OnBroadcastMessages_"); _mreShutdown.Set(); this.Start(baseAddress, path); }
private AppNotifyServiceFactory(IExecutorService executorService, string baseAddress, string path, Type[] knownTypes) : base(executorService) { _queue = new BlockingBoundQueueUser <AppNotifyData>(executorService, -1); Extensions.CreateThreadAndStart(new ThreadStart(this.OnBroadcastMessages), "AppNotifyServiceFactory_OnBroadcastMessages_"); _mreShutdown.Set(); this.Start(baseAddress, path); }
private void Initialize(bool checkItemCount) { ModuleProc PROC = new ModuleProc(DYN_MODULE_NAME, "Initialize"); try { _uniqueKey = "QueueThreadsThreadPoolExecutor_" + Guid.NewGuid().ToString(); Log.InfoV(PROC, "{0} : Capacity : {1:D}, Queue Capacity : {2:D}", _uniqueKey, this.Capacity, this.QueueCapacity); this.RegisterForShutdown(); this.ExecutorService.AddExecutor(this); _workerThreads = new List <ThreadExecutor <T> >(); _activeWorkersCount = new SortedDictionary <string, ThreadHashValue>(StringComparer.InvariantCultureIgnoreCase); _workerThreadItems = new SortedDictionary <int, int>(); _rnd = new Random(0); for (int i = 0; i < this.Capacity; i++) { IThreadSafeQueue <ThreadExecutorItem <T> > threadQueue = null; if (_kernelModeQueue) { threadQueue = new BlockingBoundQueueKernel <ThreadExecutorItem <T> >(this.ThreadExecutorService, this.QueueCapacity); } else { threadQueue = new BlockingBoundQueueUser <ThreadExecutorItem <T> >(this.ThreadExecutorService, this.QueueCapacity); } ThreadExecutor <T> executor = new ThreadExecutor <T>(this.ThreadExecutorService, threadQueue, checkItemCount, string.Empty); executor.ProcessItem += new ExecutorProcessItemHandler <T>(OnExecutor_ProcessItem); executor.ProcessItemCompleted += new ExecutorProcessItemCompletedHandler <T>(OnExecutor_ProcessItemCompleted); executor.ContainerIndex = i; _workerThreads.Add(executor); _activeWorkersCount.Add(executor.UniqueKey, new ThreadHashValue() { Executor = executor, ItemCount = 0 }); if (!_workerThreadItems.ContainsKey(i)) { _workerThreadItems.Add(i, 0); } } } catch (Exception ex) { Log.Exception(PROC, ex); } }
private void Initialize(bool kernelModeQueue) { ModuleProc PROC = new ModuleProc(DYN_MODULE_NAME, "Initialize"); try { _uniqueKey = "FreeThreadPoolExecutor_" + Guid.NewGuid().ToString(); this.RegisterForShutdown(); this.ExecutorService.AddExecutor(this); Log.InfoV(PROC, "{0} : Capacity : {1:D}", _uniqueKey, this.Capacity); _workersFree = new LinkedList <ThreadExecutor <T> >(); _workersRunning = new SortedDictionary <string, ThreadExecutor <T> >(StringComparer.InvariantCultureIgnoreCase); _workerSequences = new SortedDictionary <string, List <ThreadExecutorItem <T> > >(StringComparer.InvariantCultureIgnoreCase); for (int i = 0; i < this.Capacity; i++) { IThreadSafeQueue <ThreadExecutorItem <T> > threadQueue = null; if (kernelModeQueue) { threadQueue = new BlockingBoundQueueKernel <ThreadExecutorItem <T> >(this.ExecutorService, 1); } else { threadQueue = new BlockingBoundQueueUser <ThreadExecutorItem <T> >(this.ExecutorService, 1); } ThreadExecutor <T> executor = new ThreadExecutor <T>(this.ExecutorService, threadQueue); executor.ProcessItem += new ExecutorProcessItemHandler <T>(OnExecutor_ProcessItem); executor.ProcessItemCompleted += new ExecutorProcessItemCompletedHandler <T>(OnExecutor_ProcessItemCompleted); _workersFree.AddLast(new LinkedListNode <ThreadExecutor <T> >(executor)); } } catch (Exception ex) { Log.Exception(PROC, ex); } }
internal ThreadExecutor(IExecutorService executorService, IThreadSafeQueue <ThreadExecutorItem <T> > queue) : this(executorService, queue, false, string.Empty) { }
private void ListenMonitorQueues() { ModuleProc PROC = new ModuleProc(DYN_MODULE_NAME, "ListenMonitorQueues"); bool canShutdown = true; // Start the queues try { foreach (WorkerInfo workerInfo in _workerInfos.Values) { workerInfo.Processor.ListenAsync(); this.ExecutorService.WaitForShutdown(); } } catch (Exception ex) { Log.Exception(PROC, ex); } // listen for queues try { int length = _queues.Count; // more queues found if (length > 1) { while (!this.ExecutorService.WaitForShutdown()) { for (int i = 0; i < length; i++) { IThreadSafeQueue <QueueMessageWrapper> queue = _queues[i]; bool hasDataInPriorityQueue = false; while (queue.QueueCount > 0) { for (int j = 0; j < i; j++) { // if any items available in the the previous queue if (_queues[j].QueueCount > 0) { hasDataInPriorityQueue = true; break; } if (this.ExecutorService.WaitForShutdown()) { break; } } if (hasDataInPriorityQueue || this.ExecutorService.IsShutdown) { break; } try { _poolExecutor.QueueWorkerItem(queue.Dequeue()); } catch (Exception ex) { Log.Exception(PROC, ex); } if (hasDataInPriorityQueue || this.ExecutorService.WaitForShutdown()) { break; } } if (hasDataInPriorityQueue || this.ExecutorService.WaitForShutdown()) { break; } } } } else // single queue is enough { if (!this.MQUseWorkerThread) { canShutdown = false; } else { IThreadSafeQueue <QueueMessageWrapper> queue = _queues[0]; while (!this.ExecutorService.WaitForShutdown()) { try { _poolExecutor.QueueWorkerItem(queue.Dequeue()); } catch (Exception ex) { Log.Exception(PROC, ex); } } } } } catch (Exception ex) { Log.Exception(PROC, ex); } finally { if (canShutdown) { this.Shutdown(); } } }