Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a good way to implement the Asynchronous Programming Model?

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.

like image 689
BFree Avatar asked Nov 14 '22 12:11

BFree


1 Answers

I think that's a good way to implement the APM, but at the moment you'd get an error with more than one asynchronous call per instance.

Why don't you just externalise the delegate usage?

What I mean is, than if you just put the actual logic in your class, but then let anyone who calls your method do:

MyClass c = new MyClass();
Func<int, int> f = c.DoSomeWork;
IAsyncResult ar1 = f.BeginInvoke(1, null, null);
IAsyncResult ar2 = f.BeginInvoke(2, null, null);
//...

I guess what i'm advocating is not even implementing the APM yourself, but just recommending to people that call your method that they use the APM that's built into delegates.

like image 119
Rob Fonseca-Ensor Avatar answered May 13 '23 15:05

Rob Fonseca-Ensor