Parallel类是对线程的抽象,提供数据与任务的并行性,Parallel.Invoke()方法允许同时调用不同的方法,
//
// 摘要:
// 尽可能并行执行提供的每个操作。
//
// 参数:
// actions:
// 要执行的 System.Action 数组。
//
// 异常:
// T:System.ArgumentNullException:
// actions 参数为 null。
//
// T:System.AggregateException:
// 时,将引发的异常中的任何操作 actions 数组将引发异常。
//
// T:System.ArgumentException:
// actions 数组包含 null 元素。
public static void Invoke(params Action[] actions);
例如如下代码:
public static void Main()
{
Action actionRun = new Action(Run);
Action startAction = new Action(Start);
Action executeAction = new Action(Execute);
Parallel.Invoke(actionRun, startAction, executeAction);
}
private static void Run()
{
Console.WriteLine("this is RunAction");
}
private static void Start()
{
Console.WriteLine("this is StartAction");
}
private static void Execute()
{
Console.WriteLine("this is ExecuteAction");
}
返回结果:
由此可见确实是并行执行的。