Cómo y cuándo usar ‘async’ y ’wait ’

Desde mi entendimiento una de las principales cosas queasync yawait ¿Lo que hay que hacer es hacer que el código sea fácil de escribir y leer, pero usarlos es igual a generar subprocesos de fondo para realizar una lógica de larga duración?

Actualmente estoy probando el ejemplo más básico. He añadido algunos comentarios en línea. ¿Me lo puedes aclarar?

// 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;
}

Respuestas a la pregunta(21)

Su respuesta a la pregunta