Skip to content

Instantly share code, notes, and snippets.

@caviles
Created April 23, 2017 06:11
Show Gist options
  • Save caviles/6dd359c421e95e096e0c046bf5f05383 to your computer and use it in GitHub Desktop.
Save caviles/6dd359c421e95e096e0c046bf5f05383 to your computer and use it in GitHub Desktop.
avoid .Result Or Wait on task as it will block threads and can lead to deadlocks
always wrap your calls in try catch as the async errors are generally swallowed by the framework
public async Task<Customer> Get(int id)
{
// you are on a particular thread here
var customer = await SomeAsyncFunctionThatGetsCustomer(id).ConfigureAwait(false); // doesn't have find the orginal thread it used so is much faster
// now you are on a different thread! will that cause problems?
return customer;
}
@caviles
Copy link
Author

caviles commented Apr 23, 2017

Task.Run(() => DoWorkAsync()); //can be awaited will create a new thread

Use concurrent collections when working with threads and collections because - for instance instead of queue use concurrentqueue

@caviles
Copy link
Author

caviles commented Apr 24, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment