Created
May 30, 2012 08:51
-
-
Save mastoj/2834675 to your computer and use it in GitHub Desktop.
Eksempel på Parallel
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.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