public static MailboxProcessor <T> Start <T>(Func <MailboxProcessor <T>, Task> body, CancellationToken?cancellationToken = null) where T : class { var mailboxProcessor = new MailboxProcessor <T>(body, cancellationToken); mailboxProcessor.Start(); return(mailboxProcessor); }
/// <summary> /// Starts the agent execution, after it has been created. In a concurrent environment, the agent that sends the first message(s) /// and thus initiates the execution of the whole protocol should be started last, after all the agents have been added to the environment. /// First, the Setup method is called, and then the Act method is automatically called when the agent receives a message. /// </summary> public void Start() { if (Environment == null) { if (this.Name != null) { throw new Exception("Environment is null in agent " + this.Name + " (ConcurrentAgent.Start)"); } else { throw new Exception("Environment is null in agent (ConcurrentAgent.Start)"); } } _mbProc = MailboxProcessor.Start <Message>(async mb => { Setup(); while (true) { Message message = await mb.Receive(); Act(message); } }); }