Created
December 15, 2020 10:48
-
-
Save vshapenko/4dca69b446d5a9fcc6eebcf0d081177a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Learn more about F# at http://docs.microsoft.com/dotnet/fsharp | |
open System | |
open System.Threading | |
// Define a function to construct a message to print | |
type Msg = | |
| Print of string | |
[<EntryPoint>] | |
let main argv = | |
let mutable skipMessages = false | |
let inbox = | |
MailboxProcessor.Start( fun b -> | |
let rec loop() = async{ | |
match! b.Receive() with | |
| Print msg -> | |
if(skipMessages) | |
then | |
printfn "skipping message %s" msg | |
return! loop() | |
else | |
do! Async.Sleep 1000 | |
if(skipMessages) then () else | |
printfn "%s" msg | |
return! loop() | |
} | |
loop() | |
) | |
[1..10] |> Seq.iter (fun x -> string x |> Print |> inbox.Post ) | |
async{ | |
do! Async.Sleep 2500 | |
skipMessages <- true | |
(inbox:>IDisposable).Dispose() | |
inbox.Post (Print "11") | |
printfn "Mailbox disposed" | |
printfn "cts cancelled, queue count %i" inbox.CurrentQueueLength | |
} |> Async.Start | |
Console.ReadLine() |> ignore | |
0 // return an integer exit code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment