Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Parallel.For?

I want to use Parallel Programming in my project (WPF) . here is my for loop code.

for (int i = 0; i < results.Count; i++)
{
    product p = new product();

    Common.SelectedOldColor = p.Background;
    p.VideoInfo = results[i];
    Common.Products.Add(p, false);
    p.Visibility = System.Windows.Visibility.Hidden;
    p.Drop_Event += new product.DragDropEvent(p_Drop_Event);
    main.Children.Add(p);
}

it works without any problem. I want to write it with Parallel.For and I wrote this

Parallel.For(0, results.Count, i =>
{
    product p = new product();
    Common.SelectedOldColor = p.Background;
    p.VideoInfo = results[i];
    Common.Products.Add(p, false);
    p.Visibility = System.Windows.Visibility.Hidden;
    p.Drop_Event += new product.DragDropEvent(p_Drop_Event);
    main.Children.Add(p);
});

But an error occours in constructor of producd class is

The calling thread must be STA, because many UI components require this.

Well then I used a Dispatcher . here is code

Parallel.For(0, results.Count, i =>
{
    this.Dispatcher.BeginInvoke(new Action(() =>
        product p = new product();
        Common.SelectedOldColor = p.Background;
        p.VideoInfo = results[i];
        Common.Products.Add(p, false);
        p.Visibility = System.Windows.Visibility.Hidden;
        p.Drop_Event += new product.DragDropEvent(p_Drop_Event);
        main.Children.Add(p)));
});

I get error because of my "p" object. it expect ";" and also it says for product class; class name is not valid at this point. Then I created product object above Parallel.For, but still I get error..

How can I fix my errors?

like image 255
unbalanced Avatar asked Aug 14 '12 12:08

unbalanced


People also ask

How does parallel for work?

parallel. for loops run iterations of the loop in different processes in parallel. You can only use this if iterations are independent of one another. Only if the iterations are independent, you can assume the same results will be produced by a parallel and non-parallel for loop.

What is parallel for loop?

Parallel loops are one of the most widely used concepts to express parallelism in parallel languages and libraries. In general, a parallel loop is a loop whose iterations are executed at least partially concurrently by several threads or processes.

How do you parallel loop in C#?

There are multiple overloaded versions of the Parallel For loop available in C#. In our example, we use the following overloaded versions. public static ParallelLoopResult For(int fromInclusive, int toExclusive, Action<int> body): This method is used to execute a for loop in which iterations may run in parallel.


2 Answers

The simple answer is that you're attempting to work with components that require Single threading, more specifically it looks like they only want to run on the UI thread. So using Parallel.For is not going to be useful to you. Even when you use the dispatcher, you're just marshaling the work over to the single UI thread which negates any benefits from Parallel.For.

like image 169
CodingGorilla Avatar answered Oct 13 '22 14:10

CodingGorilla


You cannot interact with the UI from background threads.

Therefore, you cannot use Parallel.For to manage UI.

like image 36
SLaks Avatar answered Oct 13 '22 12:10

SLaks