25

I need a method that returns a Task<string> with empty string like

public static Task<string> AsyncTest()
{    
     return new Task<string>(() => string.Empty); //problem here

     // this method would work:
     // return new WebClient().DownloadStringTaskAsync(@"http://www.google.de");   
}

public static void Workdl(string input)
{
    Console.Write("OUT: " + input.Substring(0, 100));
}

This snippet compiles but when I call it like

Task<string> dlTask = AsyncTest();
Workdl(await dlTask);
await Task.WhenAll(dlTask); //Task never completes 

it never determines.

c0rd
  • 1,329
  • 1
  • 13
  • 20

2 Answers2

53

Unless you're writing your own task management system you should probably never use new Task(...).

However, that aside, the reason why this doesn't work in this case is because a new Task(...) doesn't start by itself. It just constructs the task object around your delegate.

You should either explicitly start it:

var t = new Task(() => string.Empty);
t.Start();
return t;

Or simply use Task.Run instead:

return Task.Run(() => string.Empty);

(this would be my recommendation to avoid using new Task(...))

Now, in this case I would go for something different altogether.

If you don't actually need to run the task, you can simply create an already completed task around an existing result. The reason I say "if" is that your example in the question may be dumbed down for the sake of this question and you may actually have code in there, in which case you should go for the code above this paragraph. However, if you need to adhere to an async method signature returning a task but doesn't actually need to spin up a task, then do this:

return Task.FromResult(string.Empty);

This simply "starts out already completed".

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
29

You can use Task.FromResult()

return Task.FromResult(string.Empty);
Scott Perham
  • 2,410
  • 1
  • 10
  • 20