class DemoParallel {
static void Main() {
Console.WriteLine(" .");
// , -.
Parallel.Invoke(() => {
Console.WriteLine(" #1 ");
for(int count = 0; count < 5; count++) {
Thread.Sleep(500);
Console.WriteLine(" #1 " + count);
}
Console.WriteLine(" #1 ");
},
=> {
Console.WriteLine(" #2 ");
for (int count = 0; count < 5; count++) {
Thread.Sleep(500);
Console.WriteLine(" #2 " + count);
}
Console.WriteLine(" .");
}
}
, .
For ()
TPL , , For (), Parallel. . , :
Public static ParallelLoopResult
For(int fromlnclusive, int toExclusive, Action<int> body)
fromlnclusive , ; , , ; a toExclusive , . . , fromlnclusive toExclusive . , body. Action<int>, .
public delegate void Action<in T>(T obj)
For () , , int. , obj, . , body, . For () ParallelLoopResult, . . ( .)
For () , , , . , , . , , . , , , - , - , , .
For () . data, 1000000000 . For (), "" MyTransf (). , data. . , , - . .
|
|
11 Parallel.For() // .
Using System;