Async pending return task

Can someone explain what this means in a synchronous method? If I try to change the method to async , then VS complains about it.

It works:

 public Task MethodName() { return Task.FromResult<object>(null); } 

This does not work:

 public async Task MethodName() { return Task.FromResult<object>(null); } 

So basically I would like to know what exactly this means: Task.FromResult<object>(null);

+45
c # asynchronous async-await
Aug 07 '14 at 20:22
source share
3 answers

async methods are different from regular methods. Everything that you return from async methods is wrapped in Task .

If you do not return the value (void), it will be wrapped in Task , if you return int , it will be wrapped in Task<int> , etc.

If your asynchronous method needs to return an int , you must mark the type of the returned method as Task<int> , and you will return a plain int , not Task<int> . The compiler will convert int to Task<int> for you.

 private async Task<int> MethodName() { await SomethingAsync(); return 42;//Note we return int not Task<int> and that compiles } 

Sameway, When you return the Task<object> , the return type of the method should be Task<Task<object>>

 public async Task<Task<object>> MethodName() { return Task.FromResult<object>(null);//This will compile } 

Since your method returns Task , it should not return any value. Otherwise, it will not compile.

 public async Task MethodName() { return;//This should work but return is redundant and also method is useless. } 

Keep in mind that an async method without an await statement is not async .

+83
Aug 07 '14 at 20:37
source share

You need to use the await keyword when using async, and the return type of the function must be shared. The following is an example with a return value:

 public async Task<object> MethodName() { return await Task.FromResult<object>(null); } 

Here is an example with no return value:

 public async Task MethodName() { await Task.CompletedTask; } 

Read this data:

TPL: http://msdn.microsoft.com/en-us/library/dd460717(v=vs.110).aspx and Tasks: http://msdn.microsoft.com/en-us/library/system.threading .tasks (v = vs. 110) .aspx

Async: http://msdn.microsoft.com/en-us/library/hh156513.aspx Waiting: http://msdn.microsoft.com/en-us/library/hh156528.aspx

+9
Aug 07 '14 at 20:33
source share

Adding the async keyword is just syntactic sugar to simplify the creation of a state machine. Essentially, the compiler takes your code,

 public async Task MethodName() { return null; } 

And turns it into;

 public Task MethodName() { return Task.FromResult<object>(null); } 

If your code has any await keywords, the compiler must accept your method and turn it into a class to represent the state machine needed to execute it. For each await keyword, the state of the variables and the stack will be stored in the fields of the class, the class will add itself as a completion hook to the expected task, and then return.

When this task is completed, your task will be completed again. Therefore, some additional code is added to the top of the method to restore the state of variables and go to the next panel of your code.

See What is Asynchronous and Pending to Generate? for example gory.

This process has much in common with how the compiler processes iterator methods using yield statements.

+8
Apr 28 '17 at 3:50
source share



All Articles