locked
Parallel.ForEach is not working C# RRS feed

  • Question

  • please run my program and tell me why the url is not getting added to listbox. i found also that my program is also freezing. please guide me what i made wrong to use  Parallel.ForEach ?

    does  Parallel.ForEach function may freeze the apps ? please guide. thanks

     public partial class Form1 : Form
        {
            private List<string> urls = new List<string>();
    
            public Form1()
            {
                InitializeComponent();
                urls.Add("www.test.com/a1.aspx");
                urls.Add("www.test.com/a2.aspx");
                urls.Add("www.test.com/a3.aspx");
                urls.Add("www.test.com/a4.aspx");
                urls.Add("www.test.com/a5.aspx");
                urls.Add("www.test.com/a6.aspx");
                urls.Add("www.test.com/a7.aspx");
                urls.Add("www.test.com/a8.aspx");
                urls.Add("www.test.com/a9.aspx");
                urls.Add("www.test.com/a10.aspx");
                urls.Add("www.test.com/a11.aspx");
                urls.Add("www.test.com/a12.aspx");
                urls.Add("www.test.com/a13.aspx");
                urls.Add("www.test.com/a14.aspx");
                urls.Add("www.test.com/a15.aspx");
                urls.Add("www.test.com/a16.aspx");
                urls.Add("www.test.com/a17.aspx");
                urls.Add("www.test.com/a18.aspx");
                urls.Add("www.test.com/a19.aspx");
                urls.Add("www.test.com/a20.aspx");
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Parallel.ForEach(urls, new ParallelOptions { MaxDegreeOfParallelism = 10 }, DownloadFile);
            }
    
            public void DownloadFile(string url)
            {
                if (InvokeRequired)
                {
                    listBox1.Invoke(new MethodInvoker(delegate { listBox1.Items.Add(url); }));
                }
                else
                    listBox1.Items.Add(url);
                System.Threading.Thread.Sleep(5000);
            }
        }

    Friday, May 17, 2013 8:09 PM

Answers

  • "Parallel.ForEach" blocks until all of the operations are completed, which is blocking the UI thread, which is why your application is not responsive.
    • Marked as answer by Mou_kolkata Saturday, May 18, 2013 7:27 AM
    Friday, May 17, 2013 8:47 PM
  • As Servy42 said: your Pricipal Thread (UI Thread) is busy while Parallel.ForEach is running.

    You will need to use a new Thread to call your Parallel proccess.

    See this post:

    http://stackoverflow.com/questions/8365346/why-does-this-parallel-foreach-code-freeze-the-program-up

    • Marked as answer by Mou_kolkata Saturday, May 18, 2013 7:27 AM
    Friday, May 17, 2013 9:05 PM
  • I think Parallel.ForEach can be used in the main thread. It can reduce the time to complete whole Foreach, but as you are using Windows Forms this Main Thread will not respond to the forms events while the Parallel.ForEach is processing.

    As I said, if you don´t want that the forms freeze, You need to use a Thread.

    So if you are looking parallel processing and You don´t care for the UI freezing, use just Parallel.ForEach. If don´t want UI freezing, you need to move the Parallel.Foreach to another method and create a new Thread to call it.

    If you need that UI doesn´t freezing during the processing of all download interactions, but you don´t need that each iteraction will be processing parallel (for speed), you just need to create a new a Thread.

    I don´t have another examples because I rarely use Parallel.ForEach, but I think the best use for it is to improve performance to process many iteraction itens in parallel (if you have a good hardware).

    In Web Apllication this problem doesn´t happen because during processing of the Page on server the client is already waiting for response.

    • Marked as answer by Mou_kolkata Saturday, May 18, 2013 6:38 PM
    Saturday, May 18, 2013 4:29 PM

All replies

  • "Parallel.ForEach" blocks until all of the operations are completed, which is blocking the UI thread, which is why your application is not responsive.
    • Marked as answer by Mou_kolkata Saturday, May 18, 2013 7:27 AM
    Friday, May 17, 2013 8:47 PM
  • As Servy42 said: your Pricipal Thread (UI Thread) is busy while Parallel.ForEach is running.

    You will need to use a new Thread to call your Parallel proccess.

    See this post:

    http://stackoverflow.com/questions/8365346/why-does-this-parallel-foreach-code-freeze-the-program-up

    • Marked as answer by Mou_kolkata Saturday, May 18, 2013 7:27 AM
    Friday, May 17, 2013 9:05 PM
  • r u trying to say we should not use Parallel.ForEach from the main thread rather we should us it from another thread? am i understood correctly?

    can u plzz tell me actually when one should use Parallel.ForEach ? show me few good situation. thanks

    Saturday, May 18, 2013 7:32 AM
  • r u trying to say we should not use Parallel.ForEach from the main thread rather we should us it from another thread? am i understood correctly?

    can u plzz tell me actually when one should use Parallel.ForEach ? show me few good situation. thanks

    Saturday, May 18, 2013 7:32 AM
  • I think Parallel.ForEach can be used in the main thread. It can reduce the time to complete whole Foreach, but as you are using Windows Forms this Main Thread will not respond to the forms events while the Parallel.ForEach is processing.

    As I said, if you don´t want that the forms freeze, You need to use a Thread.

    So if you are looking parallel processing and You don´t care for the UI freezing, use just Parallel.ForEach. If don´t want UI freezing, you need to move the Parallel.Foreach to another method and create a new Thread to call it.

    If you need that UI doesn´t freezing during the processing of all download interactions, but you don´t need that each iteraction will be processing parallel (for speed), you just need to create a new a Thread.

    I don´t have another examples because I rarely use Parallel.ForEach, but I think the best use for it is to improve performance to process many iteraction itens in parallel (if you have a good hardware).

    In Web Apllication this problem doesn´t happen because during processing of the Page on server the client is already waiting for response.

    • Marked as answer by Mou_kolkata Saturday, May 18, 2013 6:38 PM
    Saturday, May 18, 2013 4:29 PM
  • if u know the background of  Parallel.ForEach  that how it works internally the please share with me. does it start many thread to call a method ?

    what is the meaning of MaxDegreeOfParallelism = 10 ? does it mean that at a time maximum 10 threads will be created? maximum how many thread it can start....i mean what is the default number of thread it may start ?

    private void button1_Click(object sender, EventArgs e)
            {
                Parallel.ForEach(urls, new ParallelOptions { MaxDegreeOfParallelism = 10 }, DownloadFile);
            }
    
            public void DownloadFile(string url)
            {
                if (InvokeRequired)
                {
                    listBox1.Invoke(new MethodInvoker(delegate { listBox1.Items.Add(url); }));
                }
                else
                    listBox1.Items.Add(url);
                System.Threading.Thread.Sleep(5000);
            }


    Saturday, May 18, 2013 6:43 PM