synchronous vs asynchronous c# code example

Example: async await vs synchronous c#

Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
string urlContents = await getStringTask;
      
                 VS
string urlContents = await client.GetStringAsync();

Answer:
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.

Tags:

Misc Example