How is async with await different from a synchronous call?

Calling await client.GetStringAsync() yields the execution to the calling method, which means it won’t wait for the method to finish executing, and thus won’t block the thread. Once it’s done executing in the background, the method will continue from where it stopped.

If you just call client.GetString(), the thread’s execution won’t continue until this method finished executing, which will block the thread and may cause the UI to become unresponsive.

Example:

public void MainFunc()
{
    InnerFunc();
    Console.WriteLine("InnerFunc finished");
}

public async Task InnerFunc()
{
    // This causes InnerFunc to return execution to MainFunc,
    // which will display "InnerFunc finished" immediately.
    string urlContents = await client.GetStringAsync();

    // Do stuff with urlContents
}

public void InnerFunc()
{
    // "InnerFunc finished" will only be displayed when InnerFunc returns,
    // which may take a while since GetString is a costly call.
    string urlContents = client.GetString();

    // Do stuff with urlContents
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)