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
  • 236,525
  • 50
  • 385
  • 514
road242
  • 2,492
  • 22
  • 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
  • 15,209
  • 2
  • 27
  • 33