Note that there are some explanatory texts on larger screens.

plurals
  1. POIs this a good way to implement the Asynchronous Programming Model?
    primarykey
    data
    text
    <p>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:</p> <pre><code>//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) { } </code></pre> <p>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:</p> <pre><code>public class MyClass { private Func&lt;int, int&gt; 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&lt;int, int&gt;(DoSomeWork); var asyncResult = this.func.BeginInvoke(arg,callback,object); return asyncResult; } public int EndDoSomeWork(IAsyncResult result) { return this.func.EndInvoke(result); } } </code></pre> <p>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. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload