Created
May 14, 2012 16:31
-
-
Save jamesmanning/2694937 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
var instances = new AbstractBase[] | |
{ | |
new RegularTaskApproach(), | |
new AsyncAwaitApproach(), | |
new RegularTaskDerivedFromAsyncAwaitApproach(), | |
new RegularTaskUsingBaseAsyncAwaitApproach(), | |
}; | |
var results = | |
from instance in instances | |
let task = instance.DoSomethingAsync() | |
select new | |
{ | |
instance.GetType().Name, | |
task.Result, | |
}; | |
Console.WriteLine ("Results:\n{0}", String.Join("\n", results)); | |
} | |
// Define other methods and classes here | |
public abstract class AbstractBase | |
{ | |
public abstract Task<string> DoSomethingAsync(); | |
} | |
public class RegularTaskApproach : AbstractBase | |
{ | |
public override Task<string> DoSomethingAsync() | |
{ | |
return Task.Factory.StartNew(() => "i'm a regular task"); | |
} | |
} | |
public class AsyncAwaitApproach : AbstractBase | |
{ | |
public override async Task<string> DoSomethingAsync() | |
{ | |
return await Task.Factory.StartNew(() => "i used await!"); | |
} | |
} | |
public class RegularTaskDerivedFromAsyncAwaitApproach : AsyncAwaitApproach | |
{ | |
public override Task<string> DoSomethingAsync() | |
{ | |
return Task.Factory.StartNew(() => "i'm a regular task derived from async impl"); | |
} | |
} | |
public class RegularTaskUsingBaseAsyncAwaitApproach : AsyncAwaitApproach | |
{ | |
public override Task<string> DoSomethingAsync() | |
{ | |
var taskFromAsyncParent = base.DoSomethingAsync(); | |
var taskAddingOnSomething = taskFromAsyncParent | |
.ContinueWith(t => t.Result + " ... and i helped!"); | |
return taskAddingOnSomething; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment