c# - 在 WinForms 上使用 TPL 进行并行编程

标签 c# wpf winforms task-parallel-library

我尝试在 WinForms .NET 4.0 上使用 TPL,我遵循了 this适用于 WPF 的步骤(转到文章末尾)并做了一些小的更改,因此它可以在 WinForms 上工作,但它仍然不起作用。它应该在标签和 richTextBox 上显示结果,但它不是......我认为并行处理工作导致鼠标在我单击按钮时开始缓慢移动一段时间。

public static double SumRootN(int root)
{   double result = 0;
    for (int i = 1; i < 10000000; i++)
    {   result += Math.Exp(Math.Log(i) / root);}
    return result;
}
private void button1_Click(object sender, EventArgs e)
{   richTextBox1.Text = "";
    label1.Text = "Milliseconds: ";
    var watch = Stopwatch.StartNew();
    List<Task> tasks = new List<Task>();
    for (int i = 2; i < 20; i++)
    {   int j = i;
        var t = Task.Factory.StartNew
          (   () =>
                {   var result = SumRootN(j);
                    Dispatcher.CurrentDispatcher.BeginInvoke
                        (new Action
                             (   () => richTextBox1.Text += "root " + j.ToString() 
                                   + " " + result.ToString() + Environment.NewLine
                             )
                         , null
                        );
                 }
            );
        tasks.Add(t);
    }
    Task.Factory.ContinueWhenAll
         (  tasks.ToArray()
            , result =>
                {   var time = watch.ElapsedMilliseconds;
                    Dispatcher.CurrentDispatcher.BeginInvoke
                          (   new Action
                                (    () =>
                                      label1.Text += time.ToString()
                                 )
                           );
                }
        );
}

最佳答案

您的代码将无法工作,因为线程 UI 显示结果与 WPF 完全不同。 对于 WPF,线程 UI 是 Dispatcher,但在 Windows Form 上是另一个。

我已经修改了你的代码以帮助它工作。

    private void button1_Click(object sender, EventArgs e)
    {
        richTextBox1.Text = "";
        label1.Text = "Milliseconds: ";

        var watch = Stopwatch.StartNew();
        List<Task> tasks = new List<Task>();
        for (int i = 2; i < 20; i++)
        {
            int j = i;
            var t = Task.Factory.StartNew(() =>
            {
                var result = SumRootN(j);
                richTextBox1.Invoke(new Action(
                        () =>
                        richTextBox1.Text += "root " + j.ToString() + " " 
                              + result.ToString() + Environment.NewLine));
            });
            tasks.Add(t);
        }

        Task.Factory.ContinueWhenAll(tasks.ToArray(),
              result =>
              {
                  var time = watch.ElapsedMilliseconds;
                  label1.Invoke(new Action(() => label1.Text += time.ToString()));
              });
    }

关于c# - 在 WinForms 上使用 TPL 进行并行编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15998459/

相关文章:

c# - 使用键值对作为参数

c# - 如何在 Windows 通用应用程序中使用自定义字体?

c# - 如何从MobileServiceUser获取用户名、电子邮件等?

c# - 一个用于 UserControl 和 Window 的 ViewModel 或单独的 ViewModel

c# - 如何将组件的大小链接到窗口大小?

asp.net - 嵌入在 WinForm 应用程序中的 CassiniDev?

c# - 为什么这个 reqpro40.applicationclass 实例创建失败?

c# - Linq 交叉表和分组

WPF Listview 项目对齐

c# - 在事件链接时消除顺序依赖性和冗余的干净方法是什么?