private static Task QueueTask(this IWorkItemScheduler scheduler, Func <Task> taskFunc, IGrainContext targetContext) { var workItem = new AsyncClosureWorkItem(taskFunc, targetContext); scheduler.QueueWorkItem(workItem); return(workItem.Task); }
private static Task QueueNamedTask(this IWorkItemScheduler scheduler, Func <Task> taskFunc, IGrainContext targetContext, string activityName = null) { var workItem = new AsyncClosureWorkItem(taskFunc, activityName, targetContext); scheduler.QueueWorkItem(workItem); return(workItem.Task); }
/// <summary> /// Execute a closure ensuring that it has a runtime context (e.g. to send messages from an arbitrary thread) /// </summary> /// <param name="scheduler"></param> /// <param name="action"></param> /// <param name="targetContext"></param> private static Task RunOrQueueAction(this IWorkItemScheduler scheduler, Action action, IGrainContext targetContext) { return(scheduler.RunOrQueueTask(() => { action(); return Task.CompletedTask; }, targetContext)); }
private static Task RunOrQueueTask(this IWorkItemScheduler scheduler, Func <Task> taskFunc, IGrainContext targetContext) { var currentContext = RuntimeContext.Current; if (currentContext is object && currentContext.Equals(targetContext)) { try { return(taskFunc()); } catch (Exception exc) { return(Task.FromResult(exc)); } } return(scheduler.QueueTask(taskFunc, targetContext)); }
private static Task QueueActionAsync(this IWorkItemScheduler scheduler, Action action) { var resolver = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously); Action syncFunc = () => { try { action(); resolver.TrySetResult(true); } catch (Exception exc) { resolver.TrySetException(exc); } }; scheduler.QueueAction(syncFunc); return(resolver.Task); }
private static Task <T> RunOrQueueTask <T>(this IWorkItemScheduler scheduler, Func <Task <T> > taskFunc, IGrainContext targetContext) { var currentContext = RuntimeContext.Current; if (currentContext is object && currentContext.Equals(targetContext)) { try { return(taskFunc()); } catch (Exception exc) { var resolver = new TaskCompletionSource <T>(TaskCreationOptions.RunContinuationsAsynchronously); resolver.TrySetException(exc); return(resolver.Task); } } var workItem = new AsyncClosureWorkItem <T>(taskFunc, targetContext); scheduler.QueueWorkItem(workItem); return(workItem.Task); }