Home How and When to use `async` and `await`
Reply: 0

How and When to use `async` and `await`

user985 Published in March 20, 2018, 5:35 am

From my understanding one of the main things that async and await do is to make code easy to write and read - but is using them equal to spawning background threads to perform long duration logic?

I'm currently trying out the most basic example. I've added some comments inline. Can you clarify it for me?

// 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?
    return 1;
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.419011 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO