2

I'm totally green with TPL and want to execute an async method in a console application.

My code:

    static void Main()
    {
        Task<string> t = MainAsync();
        t.Wait();
        Console.ReadLine();
    }

    static async Task<string> MainAsync()
    {
        var result = await (new Task<string>(() => { return "Test"; }));
        return result;

    }

This task runs forever. Why? What am I missing?

svick
  • 225,720
  • 49
  • 378
  • 501
road242
  • 2,452
  • 21
  • 30

1 Answers1

8

You don't start your task. This is why Wait doesn't return. Try

var result = await Task.Run<string>(() => { return "Test"; });
EZI
  • 14,888
  • 2
  • 26
  • 32