How to create my own asynchronous method based on Task ?
-
Tuesday, February 28, 2012 6:28 AM
Suppose these is a method that i want to execute it asynchronously , for that i have
created a method like this :
public Task DoAsync(int x) { double sqrt = 0.0; for (int i = 0; i < 999999999; i++) { } Task t = new Task(()=> { sqrt = Math.Sqrt(x); }); t.Start(); return t; }
This method is called from Button's Click event like this :
private async void button1_Click(object sender, RoutedEventArgs e) { await DoAsync(6); }But this method call is not actually asynchronous.
Can anyone please tell how to define my own Task based method that can be executed asynchronously ?
All Replies
-
Tuesday, February 28, 2012 11:34 AM
public Task<int> DoAsync(int x) { return TaskEx.Run(() => { //do your stuff }); }
try this one. -
Tuesday, February 28, 2012 6:00 PM
Even if i am not using async and await keywords DoAsync() method is executed asynchronously ! Why ?
private void button1_Click(object sender, RoutedEventArgs e) { DoAsync(5); } public Task<int> DoAsync(int x) { return TaskEx.Run<int>(() => { int i; for (i = 0; i < 999999999; i++) { } int j=i; return 0; }); }DoAsync(5) method call is asynchronous even if i am not using async and await .Why ?
I previously thought that await actually makes the code asynchronous , but here something else must is happening .
Can anyone explain me here what is happening under the hood ?
-
Tuesday, February 28, 2012 7:29 PMModerator
bootstrap2, you might want to read the document at http://www.microsoft.com/download/en/details.aspx?id=19957... I expect it'll help.
async/await do not actually force code to run on different threads. The async keyword really just tells the compiler that awaits may be used in the body, meaning that the method may complete asynchronously... it doesn't force it to complete asynchronously. If you don't have any awaits in a method marked as async, the method invocation will actually run to completion on the current thread. It's only if the body of the method awaits something that's not yet complete will the method complete asynchronously when that awaited operation completes and the continuation then runs to complete the method.
Task.Run (or TaskEx.Run in the CTP) is just schedule the work provided to it to run on the ThreadPool and is returning a Task to represent the eventual completion of that work. That's why your DoAsync method is doing the work asynchronously, because you're explicitly offloading the work to the ThreadPool.
- Proposed As Answer by Stephen Toub - MSFTMicrosoft Employee, Moderator Tuesday, February 28, 2012 7:30 PM
- Marked As Answer by Stephen Toub - MSFTMicrosoft Employee, Moderator Monday, April 09, 2012 1:33 AM
-
Wednesday, February 29, 2012 4:30 AMStephen,
i have read major parts of that document and i am highly influenced by that .But actually the document did'nt give more emphasis on more basic and intricacies of how
async and await are working. I am confused on how whole thing is working under the hood.
My assumption :
await keyword just completes the method asynchronously that is waiting for any kind of method to complete and that awaited method is doing some CPU or I/O intensive computation and is returning a Task or Taks<TResult>.
Here asynchronous completion means(according to me) method somehow terminated while it was waiting for any other asynchronous method to complete.
This somehow termination is acomplished with the help of async and await keywords.
These keywords again help when the awaited method completes and returns a Task or Task<TResult> by making a continuation on the original calling function .
My assumption if async and await is not used :
If async and await is not used as is given in my example , the origional calling code completed synchronously( does not wait for the DoAsync() method to complete) , though the DoAsync method was executed on a different thread in ThreadPool , that method did'nt blocked the main calling thread.
So , these are my partial understood and perhaps misunderstood concepts about this new TAP .
Can you please bring some light to my these issues. -
Wednesday, February 29, 2012 11:33 AMDid you try made your DoAsync() method, without use the keyword Task? Try to remove that in the call of method, and inside then too.

