为什么这个Parallel.ForEach code冻结计划吗? [英] Why does this Parallel.ForEach code freeze the program up?

查看:102
本文介绍了为什么这个Parallel.ForEach code冻结计划吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更多新手问题:

这code劫掠数量从主窗口的列表代理(我无法弄清楚如何使变量是不同功能之间的可用),并确实对每一个(简单的HttpWebRequest)的支票,然后它们添加到名为finishedProxies列表。

This code grabs a number of proxies from the list in the main window (I couldn't figure out how to make variables be available between different functions) and does a check on each one (simple httpwebrequest) and then adds them to a list called finishedProxies.

有关,当我preSS启动按钮某种原因,整个程序挂断。我是IM pression平行为每个单独行动让UI线程独立的线程,以便它的响应下?

For some reason when I press the start button, the whole program hangs up. I was under the impression that Parallel creates separate threads for each action leaving the UI thread alone so that it's responsive?

private void start_Click(object sender, RoutedEventArgs e)
        {
            // Populate a list of proxies
            List<string> proxies = new List<string>();
            List<string> finishedProxies = new List<string>();

            foreach (string proxy in proxiesList.Items)
            {
                proxies.Add(proxy);
            }

            Parallel.ForEach<string>(proxies, (i) =>
            {
                string checkResult;
                checkResult = checkProxy(i);

                finishedProxies.Add(checkResult);
                // update ui
                /*
                 status.Dispatcher.Invoke(
                  System.Windows.Threading.DispatcherPriority.Normal,
                  new Action(
                    delegate()
                    {
                        status.Content = "hello" + checkResult;
                    }
                )); */
                // update ui finished


                //Console.WriteLine("[{0}] F({1}) = {2}", Thread.CurrentThread.Name, i, CalculateFibonacciNumber(i));
            });


        }

我使用code,它的注释,以进行更改Parallel.Foreach里面的UI尝试,它使程序冻结后,启动按钮是pressed。它之前我的工作,但我用Thread类。

I've tried using the code that's commented out to make changes to the UI inside the Parallel.Foreach and it makes the program freeze after the start button is pressed. It's worked for me before but I used Thread class.

我怎样才能从Parallel.Foreach内更新的用户界面和如何让我Parallel.Foreach工作,以便不使UI冻结了,而它的工作?

How can I update the UI from inside the Parallel.Foreach and how do I make Parallel.Foreach work so that it doesn't make the UI freeze up while it's working?

这里是整个code。

推荐答案

您不能在你的UI线程开始并行处理。请参阅避免在UI线程中执行并行循环,在本页面下的示​​例。

You must not start the parallel processing in your UI thread. See the example under the "Avoid Executing Parallel Loops on the UI Thread" header in this page.

更新:或者,您可以简单地创建一个新的线程手册册,并启动内部的处理,我看到你做了。没有什么错了。

Update: Or, you can simply create a new thread manuall and start the processing inside that as I see you have done. There's nothing wrong with that too.

另外,吉姆米歇尔指出,正在访问从多个线程的名单在同一时间,所以有竞争条件存在。无论是替补 ConcurrentBag 列表,或包裹在锁定语句每次访问它们的时间内的清单。

Also, as Jim Mischel points out, you are accessing the lists from multiple threads at the same time, so there are race conditions there. Either substitute ConcurrentBag for List, or wrap the lists inside a lock statement each time you access them.

这篇关于为什么这个Parallel.ForEach code冻结计划吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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