如何使用并行编程通过并行调用一次调用多个函数?

时间:2022-11-27 13:49:01

I have a WPF application like this.

我有这样的WPF应用程序。

namespace WpfApplication1
{
 /// <summary>
/// Interaction logic for MainWindow.xaml
 /// </summary>
 public partial class MainWindow : Window
 {
public delegate void NextPrimeDelegate();
int i = 0;

public MainWindow()
{
  InitializeComponent();
}

 public void CheckNextNumber()
{
  i++;
  textBox1.Text= i.ToString();
    Dispatcher.BeginInvoke(
      System.Windows.Threading.DispatcherPriority.SystemIdle,
      new NextPrimeDelegate(this.CheckNextNumber));

 }

private void button1_Click(object sender, RoutedEventArgs e)
{
  Dispatcher.BeginInvoke(
      DispatcherPriority.Normal,
      new NextPrimeDelegate(CheckNextNumber));
  }
 }

Above code is working without problem.My question is:How can I use parallel programming to call more than one function at a time by using Parallel Invoke?

上面的代码工作没有问题。我的问题是:如何使用并行调用一次调用多个函数并行调用?

For example:I have to make something like this.

例如:我必须做这样的事情。

tr[0].Start();
tr[0].Stop(); 

1 个解决方案

#1


0  

I can't quite connect your attached code sample with your question, but I'll answer your question. Parallel.Invoke() takes in a (variable-length) list of lambdas (inline functions) as its parameters. It invokes/executes all the lambdas in parallel, and blocks execution until execution of all the lambdas are complete. For example:

我无法将您的附加代码示例与您的问题联系起来,但我会回答您的问题。 Parallel.Invoke()接受一个(可变长度)lambda(内联函数)列表作为其参数。它并行调用/执行所有lambda,并阻止执行直到所有lambdas的执行完成。例如:

Parallel.Invoke( () => Thread.Sleep(500), () => Thread.Sleep(1500), () => Thread.Sleep(200));

This would invoke all three of those functions at once (() => ... is a function declared inline that takes no parameters and returns the result of the single expression that comes afterwards.) and block (meaning that execution will not continue past that point on the caller's thread) until all three of those functions are finished exeucting. In this case, Parallel.Invoke will take 1500 milliseconds, since the longest running function is the second one, which waits for 1500 milliseconds before returning.

这将立即调用所有这三个函数(()=> ...是一个声明为内联的函数,它不接受任何参数并返回之后出现的单个表达式的结果。)和block(意味着执行不会继续执行)调用者线程上的那一点),直到完成所有这三个函数的执行。在这种情况下,Parallel.Invoke将花费1500毫秒,因为最长的运行函数是第二个,它在返回之前等待1500毫秒。

I'm not quite sure what you are trying to illustrate with your example code, but if you'd like to run Start() and Stop() in parallel (again, don't really see why you would), you can do something like Parallel.Invoke(tr[0].Start(), tr[0].Stop())

我不太确定你要用你的示例代码说明什么,但如果你想并行运行Start()和Stop()(再次,你真的不明白为什么),你可以做类似Parallel.Invoke(tr [0] .Start(),tr [0] .Stop())

#1


0  

I can't quite connect your attached code sample with your question, but I'll answer your question. Parallel.Invoke() takes in a (variable-length) list of lambdas (inline functions) as its parameters. It invokes/executes all the lambdas in parallel, and blocks execution until execution of all the lambdas are complete. For example:

我无法将您的附加代码示例与您的问题联系起来,但我会回答您的问题。 Parallel.Invoke()接受一个(可变长度)lambda(内联函数)列表作为其参数。它并行调用/执行所有lambda,并阻止执行直到所有lambdas的执行完成。例如:

Parallel.Invoke( () => Thread.Sleep(500), () => Thread.Sleep(1500), () => Thread.Sleep(200));

This would invoke all three of those functions at once (() => ... is a function declared inline that takes no parameters and returns the result of the single expression that comes afterwards.) and block (meaning that execution will not continue past that point on the caller's thread) until all three of those functions are finished exeucting. In this case, Parallel.Invoke will take 1500 milliseconds, since the longest running function is the second one, which waits for 1500 milliseconds before returning.

这将立即调用所有这三个函数(()=> ...是一个声明为内联的函数,它不接受任何参数并返回之后出现的单个表达式的结果。)和block(意味着执行不会继续执行)调用者线程上的那一点),直到完成所有这三个函数的执行。在这种情况下,Parallel.Invoke将花费1500毫秒,因为最长的运行函数是第二个,它在返回之前等待1500毫秒。

I'm not quite sure what you are trying to illustrate with your example code, but if you'd like to run Start() and Stop() in parallel (again, don't really see why you would), you can do something like Parallel.Invoke(tr[0].Start(), tr[0].Stop())

我不太确定你要用你的示例代码说明什么,但如果你想并行运行Start()和Stop()(再次,你真的不明白为什么),你可以做类似Parallel.Invoke(tr [0] .Start(),tr [0] .Stop())