public async Task <T> ExecuteTaskAsync <T>(Func <Task <T> > TaskReference) { Guid newOperationId = Guid.NewGuid(); lastOperationId = newOperationId; var accessObj = await throttler.GetFreeInstanceAsync(); try { var response = await ExecuteTaskAsyncInternal <T>(TaskReference, newOperationId); return(response); } finally { throttler.ReleaseInstance(accessObj); if (!isInPauseState && throttler.GetQueueSize() == 0) { if (lastOperationId == newOperationId) { await Task.Delay(initialDelayInMilliseconds); if (lastOperationId == newOperationId) { isInPauseState = true; } } } } }
async void ProcessLoop() { var accessObj = await AccessObjGenerator.GetFreeInstanceAsync(); if (!isOnHold) { if (TotalTaskCount > 0) { if (currentExecCounter < taskThreshold) { tempCounter = taskThreshold - currentExecCounter; iterateAndExecute(HighPriorityTasks, Math.Min(tempCounter, HighPriorityTasks.Count)); tempCounter = taskThreshold - currentExecCounter; iterateAndExecute(MediumPriorityTasks, Math.Min(tempCounter, MediumPriorityTasks.Count)); tempCounter = taskThreshold - currentExecCounter; iterateAndExecute(LowPriorityTasks, Math.Min(tempCounter, LowPriorityTasks.Count)); } if (currentExecCounter <= 0) { tempCounter = taskThreshold - currentExecCounter; iterateAndExecute(IdlePriorityTasks, Math.Min(tempCounter, IdlePriorityTasks.Count)); } } } AccessObjGenerator.ReleaseInstance(accessObj); }
public async Task <bool> Reserve() { synchronizationContext = await synchronizationHandle.GetFreeInstanceAsync(); return(synchronizationContext.IsSuccess); }