C# 5 async CTP: why is internal “state” set to 0 in generated code before EndAwait call?

Yesterday I was giving a talk about the new C# “async” feature, in particular delving into what the generated code looked like, and the GetAwaiter() / BeginAwait() / EndAwait() calls. We looked in some detail at the state machine generated by the C# compiler, and there were two aspects we couldn’t understand: Why the generated … Read more

Using ‘async’ in a console application in C# [duplicate]

This question already has answers here: Can’t specify the ‘async’ modifier on the ‘Main’ method of a console app (17 answers) Closed 5 years ago. I have this simple code: public static async Task<int> SumTwoOperationsAsync() { var firstTask = GetOperationOneAsync(); var secondTask = GetOperationTwoAsync(); return await firstTask + await secondTask; } private async Task<int> GetOperationOneAsync() … 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

How would I run an async Task method synchronously?

I am learning about async/await, and ran into a situation where I need to call an async method synchronously. How can I do that? Async method: public async Task<Customers> GetCustomers() { return await Service.GetCustomersAsync(); } Normal usage: public async void GetCustomers() { customerList = await GetCustomers(); } I’ve tried using the following: Task<Customer> task = … Read more