Created
August 16, 2016 16:45
-
-
Save goenning/58bda9a5e7e0a01d0980c869d43c1627 to your computer and use it in GitHub Desktop.
This file contains 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
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
For(0, 10, (i) => { | |
Console.WriteLine(i); | |
}); | |
//Parallel.For(0, 10, (i) => { | |
// Console.WriteLine(i); | |
//}); | |
Console.WriteLine("Finished..."); | |
Console.ReadLine(); | |
} | |
public static Task For(int from, int to, Action<int> action) | |
{ | |
List<Task> tasks = new List<Task>(); | |
for (int i = from; i < to; i++) | |
{ | |
tasks.Add(Task.Factory.StartNew(o => | |
{ | |
int number = (int)o; | |
action(number); | |
}, i)); | |
} | |
return Task.WhenAll(tasks.ToArray()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment