Last active
August 29, 2015 14:05
-
-
Save jonelf/868e12372a3908058a8c to your computer and use it in GitHub Desktop.
async await test
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
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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