How to safely call an async method in C# without await

I have an async method which returns no data: public async Task MyAsyncMethod() { // do some stuff async, don’t return any data } I’m calling this from another method which returns some data: public string GetStringData() { MyAsyncMethod(); // this generates a warning and swallows exceptions return “hello world”; } Calling MyAsyncMethod() without awaiting … Read more

Asynchronously wait for Task to complete with timeout

I want to wait for a Task<T> to complete with some special rules: If it hasn’t completed after X milliseconds, I want to display a message to the user. And if it hasn’t completed after Y milliseconds, I want to automatically request cancellation. I can use Task.ContinueWith to asynchronously wait for the task to complete (i.e. schedule an action to … Read more

Using async/await for multiple tasks

I’m using an API client that is completely asynchrounous, that is, each operation either returns Task or Task<T>, e.g: static async Task DoSomething(int siteId, int postId, IBlogClient client) { await client.DeletePost(siteId, postId); // call API client Console.WriteLine(“Deleted post {0}.”, siteId); } Using the C# 5 async/await operators, what is the correct/most efficient way to start … Read more

If my interface must return Task what is the best way to have a no-operation implementation?

In the code below, due to the interface, the class LazyBar must return a task from its method (and for argument’s sake can’t be changed). If LazyBars implementation is unusual in that it happens to run quickly and synchronously – what is the best way to return a No-Operation task from the method? I have … Read more

When to use Task.Delay, when to use Thread.Sleep?

Are there good rule(s) for when to use Task.Delay versus Thread.Sleep? Specifically, is there a minimum value to provide for one to be effective/efficient over the other? Lastly, since Task.Delay causes context-switching on a async/await state machine, is there an overhead of using it? 8 s 8 Use Thread.Sleep when you want to block the … Read more

Best practice to call ConfigureAwait for all server-side code

When you have server-side code (i.e. some ApiController) and your functions are asynchronous – so they return Task<SomeObject> – is it considered best practice that any time you await functions that you call ConfigureAwait(false)? I had read that it is more performant since it doesn’t have to switch thread contexts back to the original thread … Read more