.NET ez 2012-06-04
一般迴圈的寫法
static void Main(string[] args)
{
int x = 0;
for(int i = 0 ; i<10 ; i++)
{
x++;
}
}Parallel For寫法
static void Main(string[] args)
{
int x = 0;
Parallel.For(0, 10, i =>
{
Interlocked.Increment(ref x);
});
}Parallel Foreach寫法
static void Main(string[] args)
{
List<int> testData = new List<int>();
Parallel.ForEach(testData, (item, loopState) =>
{
System.Console.WriteLine(item);
});
}取得Parallel Thread編號
static void Main(string[] args)
{
int x = 0;
Parallel.For(0, 10, i =>
{
System.Console.WriteLine("Thread " + Thread.CurrentThread.ManagedThreadId);
});
}標籤: .NET
本文章網址:
https://www.ez2o.com/Blog/Post/csharp-Parallel-Mulit-Thread
https://www.ez2o.com/Blog/Post/10
https://www.ez2o.com/Blog/Post/csharp-Parallel-Mulit-Thread
https://www.ez2o.com/Blog/Post/10
