I am a C# programmer, but I have a question about Async Workflows in F#. Supposing I have the following class in a C# class library:
class File {
IAsyncResult BeginReadAll(string fileName, AsyncCallback callback, object state){}
string EndReadAll(IAsyncResult result){}
}
My understanding is that is possible in F# for me to make a function called ReadAllAsync that I can call like this:
async { let! rsp = ReadAllAsync() }
and that it will not block the calling thread but rather release it to the thread pool and then return on another thread when the operation finishes. I think I understand how to code this in F# using Async.Primitive, but my question is: can I call this ReadAllAsync function from C# code? If so, how do I package the F# code in a class library to access from C#?
Here's an article that shows exactly what you're asking for:
http://codebetter.com/blogs/matthew.podwysocki/archive/2008/10/15/functional-c-implementing-async-computations-in-c.aspx
However, you end up using C#'s monad syntax. Since LINQ was designed for querying, it looks a bit odd. Additionally, there's no support for try/catch or using for dispose.
The "ReadAllAsync" that you get from Async.BuildPrimitive isn't going to have the same magic without the right monad syntax. Calling it is straightforward enough, but the real utility is being able to compose it.
You're probably better off just using the C# style, unfortunately,