Skip to content

Instantly share code, notes, and snippets.

public virtual int PollOne()
{
checkIfDisposed();
return runTasks(withoutCancelToken(), maxTasks: POLLONE_RUNONE_MAX_TASKS);
}
public virtual int Poll()
{
checkIfDisposed();
return runTasks(withoutCancelToken());
}
public const int POLLONE_RUNONE_MAX_TASKS = 1;
public virtual int RunOne()
{
checkIfDisposed();
return runTasks(withGlobalCancelToken(), POLLONE_RUNONE_MAX_TASKS);
}
private CancellationToken withGlobalCancelToken()
{
//Unsafe test
[TestMethod]
public void RunOne_When_Zero_Tasks_Then_Method_Does_Not_Return()
{
const int SCHEDULE_WORK_AFTER_MS = 3000;
const double RUN_MIN_DURATION_S = 2.0;
var time = StopWatchUtils.MeasureActionTime(() =>
{
[TestMethod]
public void RunOne_When_More_Tasks_Added_Then_Only_First_Task_Is_Executed()
{
bool wasTask1Called = false;
bool wasTask2Called = false;
m_scheduler.Dispatch(() =>
{
wasTask1Called = true;
});
....
catch (OperationCanceledException e)
{
Trace.WriteLine(e);
if (m_stopCancelTokenSource.IsCancellationRequested)
{
break;
}
private int runTasksCore(CancellationToken cancellationToken)
{
bool searchForTask = true;
var usedCancellationToken = cancellationToken;
var serviceData = m_isServiceThreadFlags.Value;
while (searchForTask)
{
searchForTask = false;
private int runTasks(CancellationToken cancellationToken, int maxTasks = UNLIMITED_MAX_TASKS)
{
try
{
setCurrentThreadAsServiceAllFlags(maxTasks);
return runTasksCore(cancellationToken);
}
finally
{
resetThreadAsServiceAllFlags();
public virtual int Run()
{
checkIfDisposed();
return runTasks(withWorkCancelToken());
}
private CancellationToken withWorkCancelToken()
{
lock (m_workLockObject)
[TestMethod]
public async Task Run_When_Called_From_Multiple_Threads_Then_All_Tasks_Executed()
{
const int NUMBER_OF_SCHEDULED_TASKS = 100;
const int DEFAULT_TASK_SLEEP = 100;
const int NUMBER_OF_WORKER_THREAD = 3;
var countDownEvent = new CountdownEvent(NUMBER_OF_WORKER_THREAD);
int executedTasks = 0;