Как и когда использовать «async» и «await»

Из моего понимания одна из главных вещей, которыеasync а такжеawait сделать так, чтобы код был легко писать и читать - но использует ли он равные порождения фоновых потоков для выполнения длительной логики?

Я сейчас пробую самый простой пример. Я добавил несколько комментариев. Можете ли вы уточнить это для меня?

// I don't understand why this method must be marked as `async`.
private async void button1_Click(object sender, EventArgs e)
{
    Task<int> access = DoSomethingAsync();
    // task independent stuff here

    // this line is reached after the 5 seconds sleep from 
    // DoSomethingAsync() method. Shouldn't it be reached immediately? 
    int a = 1; 

    // from my understanding the waiting should be done here.
    int x = await access; 
}

async Task<int> DoSomethingAsync()
{
    // is this executed on a background thread?
    System.Threading.Thread.Sleep(5000);
    return 1;
}

Ответы на вопрос(21)

Ваш ответ на вопрос