I’ve recently discovered the CTP Async Library and I wanted to try to write a toy program to get familiar with the new concepts, however I’m running into an issue.
I believe the code should write out
Starting
stuff in the middle
task string
but it’s not. Here’s the code I’m running with:
namespace TestingAsync
{
class Program
{
static void Main(string[] args)
{
AsyncTest a = new AsyncTest();
a.MethodAsync();
}
}
class AsyncTest
{
async public void MethodAsync()
{
Console.WriteLine("Starting");
string test = await Slow();
Console.WriteLine("stuff in the middle");
Console.WriteLine(test);
}
private async Task<string> Slow()
{
await TaskEx.Delay(5000);
return "task string";
}
}
}
Any ideas? It would be awesome if someone knew of some good tutorials and/or videos demonstrating the concepts.
You’re calling an async method, but then just letting your application finish. Options:
Thread.Sleep(or Console.ReadLine) to yourMainmethod, so that you can sleep while the async stuff happens on background threadsTaskand wait on that from yourMainmethod.For example:
Output:
In terms of videos, I did a session on async earlier in the year at Progressive .NET – the video is online. Additionally, I have a number of blog posts about async, including my Eduasync series.
Additionally there are lots of videos and blog posts from the team at Microsoft. See the Async Home Page for lots of resources.