Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to define callback for Task based asynchronous method

Following this question, I am trying to implement an async method using the TPL, and trying to follow TAP guidelines.

I want my async method to perform a callback when it's finished. As far as I can see there are three ways I can do this.

1) Callback manually in my task delegate

public Task DoWorkAsync(DoWorkCompletedCallback completedCallback) {     return Task.Factory.StartNew(     {          //do work          //call callback manually         completedCallback();     }); } 

2) Assign callback to task in task delegate

public Task DoWorkAsync(DoWorkCompletedCallback completedCallback) {     return Task.Factory.StartNew(     {          //do work     }     ).ContinueWith(completedCallback); //assign callback to Task } 

3) Assign callback to task in caller

public Task DoWorkAsync() {     return Task.Factory.StartNew(     {          //do work     }); }  public void SomeClientCode() {     Task doingWork = DoWorkAsync();     doingWork.ContinueWith(OnWorkCompleted); } 

My gut feeling is that 3 is more correct, because it decouples the callback from the method, and means that client code can manage the task any way it sees fit (using callbacks, polling etc), which seems to be what Tasks are all about. However, what happens if DoWorkAsync() completes its work before the client code hooks up its callback?

Is there a generally accepted way to do this or is it all too new?

Is there any advantage of doing 2) over 1)?

like image 262
GazTheDestroyer Avatar asked Nov 23 '11 15:11

GazTheDestroyer


People also ask

Which method calls the async task?

A synchronous method calls an async method, obtaining a Task . The synchronous method does a blocking wait on the Task .

What is async call back?

The AsyncCallback delegate represents a callback method that is called when the asynchronous operation completes. The callback method takes an IAsyncResult parameter, which is subsequently used to obtain the results of the asynchronous operation.

What is asynchronous and synchronous callback in C#?

Sync and Async callbacks are that. Synchronous, execute on the same thread that called the method (started the action). Async are generally executed on another thread (but not always) The IAsyncCallback interface is a template to initiate an asynchronous task.

How do you call async method without await?

However, just to address "Call an async method in C# without await", you can execute the async method inside a Task. Run . This approach will wait until MyAsyncMethod finish. await asynchronously unwraps the Result of your task, whereas just using Result would block until the task had completed.


1 Answers

The generally accepted way is 3.

Users of TPL generally know that they can continue a Task with ContinueWith. 1 and 2 provide the same functionality, but with a non-standard interface; the user has to figure out what the delegate parameter means and what to pass e.g. if they don't want to continue -- all that while the method still returns a Task that can be continued in the standard way.

like image 154
dtb Avatar answered Oct 12 '22 09:10

dtb