在使用的WinForms TPL并行编程 [英] Parallel Programming using TPL on WinForms

查看:119
本文介绍了在使用的WinForms TPL并行编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用TPL上的WinForms .NET 4.0,我也跟着的这个的步骤(去文章的末尾)是WPF和做一些小的修改,以便它可以在工作的WinForms,但它仍然没有按'T工作..它应该显示导致标签和RichTextBox中上,但它不是...我认为并行处理工作原因鼠标开始了一段缓慢移动,当我按一下按钮..

 公共静态双SumRootN(INT根)
{double结果= 0;
的for(int i = 1; I<千万,我++)
{结果+ = Math.Exp(将Math.log(I)/根);}
返回结果;
}
私人无效的button1_Click(对象发件人,EventArgs五)
{richTextBox1.Text =;
label1.Text =毫秒;
无功表= Stopwatch.StartNew();
名单,LT;任务>任务=新的List<任务>();
的for(int i = 2; I< 20;我++)
{INT J =;
变种T = Task.Factory.StartNew
(()=>
{VAR的结果= SumRootN(J);
Dispatcher.CurrentDispatcher.BeginInvoke
(新行动
(()=> richTextBox1.Text + =根+ j.ToString()
++ result.ToString()+ Environment.NewLine

,空
);
}
);
tasks.Add(T);
}
Task.Factory.ContinueWhenAll
(tasks.ToArray()
,结果=>
{VAR时间= watch.ElapsedMilliseconds;
调度.CurrentDispatcher.BeginInvoke
(新动作
(()=>
label1.Text + = time.ToString()

);
}
);
}


解决方案

您的代码不会因为工作线程UI显示结果和WPF完全不同。
使用WPF线程UI是调度员,但在Windows上的形式是另外一个。



我修改了代码,以帮助它的工作。

 私人无效的button1_Click(对象发件人,EventArgs五)
{
richTextBox1.Text =;
label1.Text =毫秒;

无功表= Stopwatch.StartNew();
名单,LT;任务>任务=新的List<任务>();
的for(int i = 2; I< 20;我++)
{
INT J =;
变种T = Task.Factory.StartNew(()=>
{
VAR的结果= SumRootN(J);
richTextBox1.Invoke(新动作(
()=>
richTextBox1.Text + =根+ j.ToString()+
+ result.ToString()+ Environment.NewLine));
});
tasks.Add(T);
}

Task.Factory.ContinueWhenAll(tasks.ToArray(),
结果=>
{
VAR时间= watch.ElapsedMilliseconds;
label1.Invoke(新动作(()=> label1.Text + = time.ToString()));
});
}


I trying to use TPL on WinForms .NET 4.0, I followed this steps (go to the end of article) that are for WPF and made some small changes so it could work on WinForms but it still doesn't work.. It should display result on label and richTextBox but it not... I think the parallel process work cause mouse start moving slow for a while when I click the button..

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()
                                 )
                           );
                }
        );
}

解决方案

Your code will not work because the thread UI to display result totally different with WPF. With WPF the thread UI is Dispatcher but on Windows Form is another one.

I have modified your code to help it work.

    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()));
              });
    }

这篇关于在使用的WinForms TPL并行编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆