Skip to content

Instantly share code, notes, and snippets.

@mastoj
Created May 30, 2012 08:51
Show Gist options
  • Save mastoj/2834675 to your computer and use it in GitHub Desktop.
Save mastoj/2834675 to your computer and use it in GitHub Desktop.
Eksempel på Parallel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ParallellDemo
{
public class Worker
{
public int Id { get; set; }
public Worker(int id)
{
Id = id;
}
public void DoWork()
{
Console.WriteLine("Thread {0}: Worker {1}", Thread.CurrentThread.ManagedThreadId, Id);
}
}
class Program
{
static void Main(string[] args)
{
var workers = new List<Worker>();
for (int i = 0; i < 100; i++)
{
workers.Add(new Worker(i));
}
var tasks = workers.Select<Worker, Action>(y => y.DoWork);
Console.WriteLine("Main runs on thread {0}", Thread.CurrentThread.ManagedThreadId);
Parallel.Invoke(tasks.ToArray());
Console.WriteLine("Main runs on thread {0}", Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("All are done");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment