Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Task-based Asynchronous Pattern (TAP) with .NET 4.0 and C# 4.0

Is there a way to use the Task-based Asynchronous Pattern (TAP) in .NET 4.0 without the await and async keywords? (I ask because we are stuck in C# 4.0.)

Also, is there an example of using TAP with WCF with .NET 4.0?

like image 225
user1159441 Avatar asked Jul 01 '14 21:07

user1159441


1 Answers

Is there a way to use Task-based Asynchronous Pattern (TAP) in .NET 4.0 without the keyword await or async? I ask because we are stuck in C#4)

One notable and handy hack is to use yield and IEnumerable<Task>. E.g., from Stephen Toub's "Processing Sequences of Asynchronous Operations with Tasks":

IEnumerable<Task> DoExample(string input) 
{ 
    var aResult = DoAAsync(input); 
    yield return aResult; 
    var bResult = DoBAsync(aResult.Result); 
    yield return bResult; 
    var cResult = DoCAsync(bResult.Result); 
    yield return cResult; 
    … 
}

… 
Task t = Iterate(DoExample(“42”));

This way, you can have a pseudo-synchronous linear code flow similar to async/await.

like image 158
noseratio Avatar answered Nov 10 '22 17:11

noseratio