public static void RunTask(Func <Task> asyncAction) { var context = ActorCell.Current; if (context == null) { throw new InvalidOperationException("RunTask must be call from an actor context."); } var mailbox = context.Mailbox; //suspend the mailbox mailbox.Suspend(MailboxSuspendStatus.AwaitingTask); ActorTaskScheduler actorScheduler = context.TaskScheduler; Task <Task> .Factory.StartNew(() => asyncAction(), CancellationToken.None, TaskCreationOptions.None, actorScheduler) .Unwrap() .ContinueWith(parent => { Exception exception = GetTaskException(parent); if (exception == null) { mailbox.Resume(MailboxSuspendStatus.AwaitingTask); context.CheckReceiveTimeout(); } else { context.Self.Tell(new ActorTaskSchedulerMessage(exception), ActorRefs.NoSender); } }, actorScheduler); }
/// <summary> /// TBD /// </summary> /// <param name="asyncAction">TBD</param> /// <exception cref="InvalidOperationException"> /// This exception is thrown if this method is called outside an actor context. /// </exception> public static void RunTask(Func <Task> asyncAction) { var context = ActorCell.Current; if (context == null) { throw new InvalidOperationException("RunTask must be called from an actor context."); } var dispatcher = context.Dispatcher; //suspend the mailbox dispatcher.Suspend(context); ActorTaskScheduler actorScheduler = context.TaskScheduler; actorScheduler.CurrentMessage = context.CurrentMessage; actorScheduler.OnBeforeTaskStarted(); Task <Task> .Factory.StartNew(asyncAction, CancellationToken.None, TaskCreationOptions.None, actorScheduler) .Unwrap() .ContinueWith(parent => { Exception exception = GetTaskException(parent); if (exception == null) { dispatcher.Resume(context); context.CheckReceiveTimeout(); } else { context.Self.AsInstanceOf <IInternalActorRef>().SendSystemMessage(new ActorTaskSchedulerMessage(exception, actorScheduler.CurrentMessage)); } //clear the current message field of the scheduler actorScheduler.CurrentMessage = null; actorScheduler.OnAfterTaskCompleted(); }, actorScheduler); }