Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a MailboxProcessor in F#... I would use a SynchronizationContext in C# for the same purpose. Give me a few minutes and I will write up an example.</p> <p>Aside: Here's my code in F# that does something similar... It will be considerably more effort, but still doable in C# with Rx.</p> <pre><code>open System.Diagnostics let numWorkers = 20 let asyncDelay = 100 type MessageForMailbox = | DataMessage of AsyncReplyChannel&lt;unit&gt; | GetSummary of AsyncReplyChannel&lt;unit&gt; let main = let actor = MailboxProcessor.Start( fun inbox -&gt; let rec loop acc = async { let! message = inbox.Receive() match message with | DataMessage replyChannel -&gt; replyChannel.Reply(); return! loop acc | GetSummary replyChannel -&gt; replyChannel.Reply(); return! loop acc } loop 0 // seed for acc ) let codeBlocks = [for i in 1..numWorkers -&gt; async { do! Async.Sleep asyncDelay return! actor.PostAndAsyncReply DataMessage } ] while true do printfn "Concurrent started..." let sw = new Stopwatch() sw.Start() codeBlocks |&gt; Async.Parallel |&gt; Async.RunSynchronously |&gt; ignore actor.PostAndReply GetSummary sw.Stop() printfn "Concurrent in %d millisec" sw.ElapsedMilliseconds printfn "efficiency: %d%%" (int64 (asyncDelay * 100) / sw.ElapsedMilliseconds) printfn "Synchronous started..." let sw = new Stopwatch() sw.Start() for codeBlock in codeBlocks do codeBlock |&gt; Async.RunSynchronously |&gt; ignore sw.Stop() printfn "Synchronous in %d millisec" sw.ElapsedMilliseconds printfn "efficiency: %d%%" (int64 (asyncDelay * numWorkers * 100) / sw.ElapsedMilliseconds) main </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload