Questions tagged [c#-5.0]

For issues relating to development with C#, version 5.0.

C# is a high level, general-purpose, garbage-collected and object-oriented programming language created by Microsoft.

Version 5.0 features asynchronous methods and is currently the latest release of Microsoft's implementation, available with Visual Studio 2012 since 2012-Aug-15.

The following major features were introduced in C# 5.0:

The complete language specification is available for download from Microsoft.

789 questions
750
votes
28 answers

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 GetCustomers() { return await Service.GetCustomersAsync(); } Normal…
Rachel
  • 130,264
  • 66
  • 304
  • 490
525
votes
8 answers

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, e.g: static async Task DoSomething(int siteId, int postId, IBlogClient client) { await client.DeletePost(siteId, postId); // call…
Ben Foster
  • 34,340
  • 40
  • 176
  • 285
373
votes
3 answers

Do you have to put Task.Run in a method to make it async?

I'm trying to understand async await in the simplest form. I want to create a very simple method that adds two numbers for the sake of this example, granted, it's no processing time at all, it's just a matter of formulating an example here. Example…
Neal
  • 9,487
  • 15
  • 58
  • 101
221
votes
3 answers

How do you create an asynchronous method in C#?

Every blog post I've read tells you how to consume an asynchronous method in C#, but for some odd reason never explain how to build your own asynchronous methods to consume. So I have this code right now that consumes my method: private async void…
Khalid Abuhakmeh
  • 10,709
  • 10
  • 52
  • 75
210
votes
4 answers

Using 'async' in a console application in C#

I have this simple code: public static async Task SumTwoOperationsAsync() { var firstTask = GetOperationOneAsync(); var secondTask = GetOperationTwoAsync(); return await firstTask + await secondTask; } private async Task
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
198
votes
4 answers

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#…
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
185
votes
2 answers

How do you implement an async action delegate method?

A little background information. I am learning the Web API stack and I am trying to encapsulate all data in the form of a "Result" object with parameters such as Success and ErrorCodes. Different methods however, would produce different results and…
Albin Anke
  • 1,895
  • 2
  • 12
  • 6
175
votes
8 answers

Is there any async equivalent of Process.Start?

Like the title suggests, is there an equivalent to Process.Start (allows you run another application or batch file) that I can await? I'm playing with a small console app and this seemed like the perfect place to be using async and await but I can't…
linkerro
  • 5,318
  • 3
  • 25
  • 29
152
votes
2 answers

How does Task become an int?

We have this method: async Task AccessTheWebAsync() { HttpClient client = new HttpClient(); Task getStringTask = client.GetStringAsync("http://msdn.microsoft.com"); // You can do work here that doesn't rely on the string…
Freeman
  • 5,691
  • 3
  • 29
  • 41
142
votes
9 answers

Is it possible to "await yield return DoSomethingAsync()"

Are regular iterator blocks (i.e. "yield return") incompatible with "async" and "await"? This gives a good idea of what I'm trying to do: async Task> Method(String [] Strs) { // I want to compose the single result to the final…
jiangzhen
  • 1,542
  • 2
  • 10
  • 7
137
votes
5 answers

Using async-await on .net 4

I'm currently starting to create an application that would profit a lot from C# 5's async-await feature. But I'm not sure which version of VS and of the async runtime to use. Looking at OS popularity charts, I'll need to support Windows XP for…
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
137
votes
6 answers

How do I disable C# 6 Support in Visual Studio 2015?

Background We have a project that we're developing in VS 2015 with C#6 enabled that occasionally needs to be opened by developers using VS 2013 without C#6. We have no intention to use C# 6 within this particular solution (as much as I'd like…
SeanKilleen
  • 8,809
  • 17
  • 80
  • 133
129
votes
3 answers

Wrapping synchronous code into asynchronous call

I have a method in ASP.NET application, that consumes quite a lot of time to complete. A call to this method might occur up to 3 times during one user request, depending on the cache state and parameters that user provides. Each call takes about 1-2…
Eadel
  • 3,797
  • 6
  • 38
  • 43
128
votes
5 answers

An async/await example that causes a deadlock

I came across some best practices for asynchronous programming using c#'s async/await keywords (I'm new to c# 5.0). One of the advices given was the following: Stability: Know your synchronization contexts ... Some synchronization contexts are…
dror
  • 3,759
  • 6
  • 31
  • 45
115
votes
9 answers

Site stopped working in asp.net System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to

I have a problem like this on server [A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to [B]System.Web.WebPages.Razor.Configuration.HostSection. Type A originates from 'System.Web.WebPages.Razor, Version=2.0.0.0, …
user2632851
  • 1,169
  • 2
  • 7
  • 5
1
2 3
52 53