private void ExecuteEnqueuedActionsAsync() { lock (workQueue) { // only 1 worker if (isWorking) { return; } isWorking = true; worker.DoWork(() => { try { while (true) { // we want a new session on the thread after each action avoid creating big NH sessions using (persister) { // work while there is remaining work Work work = null; lock (workQueue) { if (workQueue.Count <= 0) { // all done, we can end return; } // next thing to do work = workQueue.Dequeue(); logger.Debug("Dequeuing " + work.Name + ", remaining: " + workQueue.Count + " on thread " + Thread.CurrentThread.ManagedThreadId); } logger.Debug("Executing " + work.Name); work.Action(); } } } finally { lock (workQueue) { // allow new worker to be spawned isWorking = false; currentWork = ""; } } }); } }
protected void Execute(Work work) { if (handleErrors) WrapActionInErrorHandling(work); if (!async) { work.Action(); return; } lock (workQueue) { logger.Debug("Enqueuing " + work.Name + ", to existing: " + workQueue.Count + " on thread " + Thread.CurrentThread.ManagedThreadId); workQueue.Enqueue(work); } ExecuteEnqueuedActionsAsync(); }
protected void Execute(Work work) { if (handleErrors) { WrapActionInErrorHandling(work); } if (!async) { work.Action(); return; } lock (workQueue) { logger.Debug("Enqueuing " + work.Name + ", to existing: " + workQueue.Count + " on thread " + Thread.CurrentThread.ManagedThreadId); workQueue.Enqueue(work); } ExecuteEnqueuedActionsAsync(); }