AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
![]() ![]() ![]() To wait for multiple Task you can use TPLs Task.WhenAll() for this, which will give you an aggregated result task, which will have a result object which contains the results from the original tasks you used in the Task.WaitAll() call. Which gives the following results when run: Sleepy |> Async.startAsPlainTask |> ignore ![]() ( fun () -> work |> Async.RunSynchronously)ĭo! Async.Sleep(5000) // sleep for 5 secondsĭo! sleepy |> Async.startAsPlainTask |> Async.awaitPlainTask Let inline startAsPlainTask (work : Async) = ![]() Task.ContinueWith continuation |> Async.AwaitTask rethrow exception from preceding task if it fauled AwaitTaskVoid function, which will now appear This extends the Async module to add the That is pretty cool actually, C# allows extension methods (which F# also allows), but being able to just add arbitrary functions is very cool. This example also demonstrates how you can extend the Async module to include your own user specified functions. Here is a small example, of how you can wait on a plain Task. There however a few tricks you can do, the first one requires a bit of insight into multi threading anyway, which is that Task, and Task for that matter both implement IAsyncResult, which is something you can wait on inside of a F# async workflow, by using Async.AwaitIAsyncResult. Task may still be waited on in C# land, but there seems to be less you can do with a standard Task (one that doesn’t return a value) in F#. That is a Task that does not return a value, basically you have Task which is a task that returns T, and Task (essentially Task void, or Task in F# lingo), which is a task that doesn’t return a value. Here are the results of running the above code:Īnother thing you might find yourself wanting to do is a use a TPL Task. Printfn "Task result is : %O" finalResult Let finalResult = Async.RunSynchronously asynWorkflow NOTE : Async.StartAsNewTask doesn't like TaskCreationOptions.LongRunning Printfn "Starting task that returns a value" |> ignore Printfn "Work loop is currently %O" i |> ignore We will the use the blocking Task.Wait() method, to obtain the result of the Task, which will be a boolean in this case. VISUAL STUDIO WINFORMS POPUP MESSAGE FROM CODE BEHIND HOW TOIn this simple example we will show how to create a simple Task that returns a boolean.
0 Comments
Read More
Leave a Reply. |