本文整理汇总了C#中System.Threading.Tasks.Parallel.Invoke方法的典型用法代码示例。如果您正苦于以下问题:C# Parallel.Invoke方法的具体用法?C# Parallel.Invoke怎么用?C# Parallel.Invoke使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.Tasks.Parallel
的用法示例。
在下文中一共展示了Parallel.Invoke方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
class ParallelInvokeDemo
{
// Demonstrated features:
// Parallel.Invoke()
// Expected results:
// The threads on which each task gets executed may be different.
// The thread assignments may be different in different executions.
// The tasks may get executed in any order.
// Documentation:
// http://msdn.microsoft.com/library/dd783942(VS.100).aspx
static void Main()
{
try
{
Parallel.Invoke(
BasicAction, // Param #0 - static method
() => // Param #1 - lambda expression
{
Console.WriteLine("Method=beta, Thread={0}", Thread.CurrentThread.ManagedThreadId);
},
delegate() // Param #2 - in-line delegate
{
Console.WriteLine("Method=gamma, Thread={0}", Thread.CurrentThread.ManagedThreadId);
}
);
}
// No exception is expected in this example, but if one is still thrown from a task,
// it will be wrapped in AggregateException and propagated to the main thread.
catch (AggregateException e)
{
Console.WriteLine("An action has thrown an exception. THIS WAS UNEXPECTED.\n{0}", e.InnerException.ToString());
}
}
static void BasicAction()
{
Console.WriteLine("Method=alpha, Thread={0}", Thread.CurrentThread.ManagedThreadId);
}
}