Recently, I am torn, trying to find out everything I can about .Net multithreading. (Improve yourself, but still feel that there is something to learn). Now I focus on APM (asynchronous programming model), which is commonly known as:
public int DoSomeWork(int arg)
{
}
public IAsyncResult BeginDoSomeWork(int arg, AsyncCallback callback, object @object)
{
}
public int EndDoSomeWork(IAsyncResult result)
{
}
Now let's say that I'm writing some kind of library, and I want to disclose this function to everyone who uses my API, I was thinking about ways to implement this template. Implementing the IAsyncResult interface is an option, but it seems rather complicated. My question is that using a delegate is an acceptable solution. I mean the following:
public class MyClass
{
private Func<int, int> func;
public int DoSomeWork(int arg)
{
}
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);
}
}
, BeginXxx EndXxx, . IAsyncResult, - , .