public Task Enqueue(Action action) { var workItem = new ActionWorkItem(action); SynchronizationContext.Post(_SendOrPostWorkItem, workItem); return(workItem.Task); }
public void DoWorkShouldCallDoWorkAction() { bool doWorkActionIsCalledIsCalled = false; ActionWorkItem actionWorkItem = new ActionWorkItem(() => doWorkActionIsCalledIsCalled = true); actionWorkItem.DoWork(); Assert.AreEqual(true, doWorkActionIsCalledIsCalled); }
public void StopShouldCallStopWorkAction() { bool stopWorkActionIsCalled = false; ActionWorkItem actionWorkItem = new ActionWorkItem(() => {}, () => stopWorkActionIsCalled = true); actionWorkItem.Stop(); Assert.AreEqual(true, stopWorkActionIsCalled); }
public static SignalFuture Invoke(this ThreadGroup group, Action action, bool forMainThread = false) { var workItem = new ActionWorkItem { Action = action, Future = new SignalFuture() }; var queue = group.GetQueueForType <ActionWorkItem>(forMainThread); queue.Enqueue(ref workItem); return(workItem.Future); }
private Task Enqueue(ActionWorkItem workitem) { try { _TaskQueue.Enqueue(workitem); return(workitem.Task); } catch (Exception) { return(TaskBuilder.Cancelled); } }
public void A() { Random random = new Random(); WorkerThreadPool workerThreadPool = new WorkerThreadPool(6 * 10 * 1000, 8, 8); const int workItemsCount = 100; int counter = 0; int totalSleepMilliseconds = 0; IList <IWorkItem> workItems = new List <IWorkItem>(workItemsCount); for (int i = 0; i < workItemsCount; i++) { ActionWorkItem workItem = new ActionWorkItem( () => { int sleepMilliseconds = random.Next(1000, 2000); Interlocked.Add(ref totalSleepMilliseconds, sleepMilliseconds); Thread.Sleep(sleepMilliseconds); Interlocked.Increment(ref counter); }); workItems.Add(workItem); } Stopwatch stopwatch = Stopwatch.StartNew(); stopwatch.Start(); for (int i = 0; i < workItems.Count; i++) { IWorkItem workItem = workItems[i]; workerThreadPool.QueueWorkItem(workItem); } workerThreadPool.Shutdown(true, 500); stopwatch.Stop(); TimeSpan totalExecutionTime = TimeSpan.Zero; for (int i = 0; i < workItems.Count; i++) { IWorkItem workItem = workItems[i]; totalExecutionTime += workItem.ExecutionTime; } totalExecutionTime.ToString(); }
public void A() { Random random = new Random(); WorkerThreadPool workerThreadPool = new WorkerThreadPool(6 * 10 * 1000, 8, 8); const int workItemsCount = 100; int counter = 0; int totalSleepMilliseconds = 0; IList<IWorkItem> workItems = new List<IWorkItem>(workItemsCount); for (int i = 0; i < workItemsCount; i++) { ActionWorkItem workItem = new ActionWorkItem( () => { int sleepMilliseconds = random.Next(1000, 2000); Interlocked.Add(ref totalSleepMilliseconds, sleepMilliseconds); Thread.Sleep(sleepMilliseconds); Interlocked.Increment(ref counter); }); workItems.Add(workItem); } Stopwatch stopwatch = Stopwatch.StartNew(); stopwatch.Start(); for (int i = 0; i < workItems.Count; i++) { IWorkItem workItem = workItems[i]; workerThreadPool.QueueWorkItem(workItem); } workerThreadPool.Shutdown(true, 500); stopwatch.Stop(); TimeSpan totalExecutionTime = TimeSpan.Zero; for (int i = 0; i < workItems.Count; i++) { IWorkItem workItem = workItems[i]; totalExecutionTime += workItem.ExecutionTime; } totalExecutionTime.ToString(); }
public void StopShouldNotExplodeWhenStopWorkActionIsNull() { ActionWorkItem actionWorkItem = new ActionWorkItem(() => {}); Assert.DoesNotThrow(() => actionWorkItem.Stop()); }