Skip to content

Instantly share code, notes, and snippets.

@goenning
Created August 16, 2016 16:45
Show Gist options
  • Save goenning/58bda9a5e7e0a01d0980c869d43c1627 to your computer and use it in GitHub Desktop.
Save goenning/58bda9a5e7e0a01d0980c869d43c1627 to your computer and use it in GitHub Desktop.
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