road242
road242

Reputation: 2532

async/await not working in console application

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?

Upvotes: 2

Views: 1252

Answers (1)

EZI
EZI

Reputation: 15364

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

var result = await Task.Run<string>(() => { return "Test"; });

Upvotes: 8

Related Questions