Skip to content

Instantly share code, notes, and snippets.

@jonelf
Last active August 29, 2015 14:05
Show Gist options
  • Save jonelf/868e12372a3908058a8c to your computer and use it in GitHub Desktop.
Save jonelf/868e12372a3908058a8c to your computer and use it in GitHub Desktop.
async await test
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
public static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
var task = test();
task.Wait();
Console.WriteLine(string.Join(", ", task.Result));
}
}
private static async Task<IEnumerable<int>> test()
{
var ids = new List<int>();
for (int i = 0; i < 10; i++)
{
ids.Add(Thread.CurrentThread.ManagedThreadId);
await Task.Delay(100);
}
return ids;
}
}
}
@jonelf
Copy link
Author

jonelf commented Aug 20, 2014

Output:

1, 4, 4, 4, 4, 4, 4, 4, 4, 4
1, 4, 4, 4, 4, 4, 4, 4, 4, 4
1, 4, 4, 4, 4, 4, 4, 4, 4, 4
1, 4, 4, 4, 4, 4, 4, 4, 4, 4
1, 4, 4, 4, 4, 4, 4, 4, 4, 4
1, 4, 5, 4, 5, 4, 5, 4, 5, 4
1, 4, 5, 4, 5, 4, 5, 4, 5, 4
1, 4, 5, 4, 5, 4, 5, 4, 5, 4
1, 4, 5, 4, 5, 4, 5, 4, 5, 4
1, 4, 5, 4, 5, 4, 5, 4, 5, 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment