Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Can a "task method" also be an "async" method?

Tags:

I'm trying to get the hand of the new async CTP stuff and I'm probably confusing myself here.. I can have this "task method", with no problem:

    public static Task<String> LongTaskAAsync() {         return Task.Run(() => {             return("AAA");             });         } 

But what if I need the task to execute another task, can I mark it as "async" and use "await"? I tried this:

public async static Task<String> LongTaskAAsync() {         await Task.Delay(2000);         return Task.Run(() => {             return("AAA");             });         } 

But then mysteriously get this compiler error: Since this is an async method, the return expression must be of type 'string' rather than Task<string>

What am I missing here?

like image 229
Michael Ray Lovett Avatar asked Jul 30 '12 00:07

Michael Ray Lovett


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


1 Answers

You may want to read my async/await intro post.

Return values from async methods are wrapped in a Task<TResult>. Likewise, await unwraps those return values:

public static async Task<String> LongTaskAAsync() {   await Task.Delay(2000);   return await Task.Run(() => {     return("AAA");   }); } 

The reasoning behind this is described in my Async "Why Do the Keywords Work That Way" Unofficial FAQ.

P.S. You can also use Task.FromResult for simple tests like this.

Edit: If you want to create and return the Task object itself, then the method should not be async. One somewhat common pattern is to have a public non-async method that calls the async portion only if necessary.

For example, some kind of asynchronous cache - if the object is in the cache, then return it immediately; otherwise, asynchronously create it, add it to the cache, and return it (this is example code - not thread-safe):

public static Task<MyClass> GetAsync(int key) {   if (cache.Contains(key))     return Task.FromResult(cache[key]);   return CreateAndAddAsync(key); }  private static async Task<MyClass> CreateAndAddAsync(int key) {   var result = await CreateAsync(key);   cache.Add(key, result);   return result; } 
like image 105
Stephen Cleary Avatar answered Nov 03 '22 21:11

Stephen Cleary