I've been on a tear lately trying to learn everything I can about .Net Multithreading. (Getting better at it, but still feel like there's lots to learn). Right now I'm focusing on the APM (Asynchronous Programming Model) which is commonly known as this:
//non async method
public int DoSomeWork(int arg)
{
//do something and return result
}
//async version
public IAsyncResult BeginDoSomeWork(int arg, AsyncCallback callback, object @object)
{
}
public int EndDoSomeWork(IAsyncResult result)
{
}
Now, let's say I'm writing some library, and I want to expose this functionality to anyone who's consuming my API, I was thinking about ways to implement this pattern. Implementing the IAsyncResult interface is a possibility, but it seems quite complex. My question is, if using delegate is an acceptable solution. What I mean by that is this:
public class MyClass
{
private Func<int, int> func;
//non async method
public int DoSomeWork(int arg)
{
//do something and return result
}
//async version
public IAsyncResult BeginDoSomeWork(int arg, AsyncCallback callback, object @object)
{
this.func = new Func<int, int>(DoSomeWork);
var asyncResult = this.func.BeginInvoke(arg,callback,object);
return asyncResult;
}
public int EndDoSomeWork(IAsyncResult result)
{
return this.func.EndInvoke(result);
}
}
Basically, every delegate has the BeginXxx and EndXxx functionality baked right into it. Is it OK to take advantage of that and just expose the IAsyncResult, or is there something wrong with this that I'm not thinking of.