Search code examples
f#agent

In F#, do agent ReplyChannels/AsyncReplyChannels cross threads?


For example, if I start an agent (1) on a UI thread, then start another agent (2) on a background thread and send a message from (1) to (2) that includes a ReplyChannel/AsyncReplyChannel and then send a reply from (2) back to (1), is that enough to ensure cross-thread communication between a background-thread agent and the UI-thread agent? Other methods for communicating with an agent on a specific thread seem to prefer using the raising of events on the specific thread's SynchronizationContext, and say nothing about using ReplyChannels...


Solution

  • When u talk about agents do u mean mailboxprocessors? In that case sending/posting messages and using replychanel is sufficient to communicate between two agents/threads.

    Pls note that the communication will NOT happening between the UI thread and the background thread u mentioned directly. Each agent/mailboxprocessor has its own thread.

    If u use "PostAndReply" from UI thread u don't need SynchronizationContext as the call is blocking internally.

    If u use "PostAndAsyncReply" an async F# workflow will be returned. In that case u still need to use SynchronizationContext. In that case pls refer to https://msdn.microsoft.com/en-us/visualfsharpdocs/conceptual/async.switchtocontext-method-%5Bfsharp%5D