6

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?

Steven
  • 151,500
  • 20
  • 287
  • 393
unbalanced
  • 1,202
  • 5
  • 17
  • 40
  • Ever tried formatting your code before posting it on Stackoverflow? – Steven Aug 14 '12 at 13:25
  • @Steven, no. I used two programs. the first one is resharper and the second one is indent guides. they make my code arrange .. really so useful tools.. – unbalanced Aug 14 '12 at 13:27
  • If you want anyone to take the time to read and answer youe question, make sure that your code is reabable (without needing to scroll for instance). Take a look at my update for your question. – Steven Aug 14 '12 at 13:36
  • 1
    @Steven,thank you for suggestion.. :) i will care about it next time – unbalanced Aug 14 '12 at 13:45

3 Answers3

8

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.

CodingGorilla
  • 18,826
  • 2
  • 41
  • 61
3

You cannot interact with the UI from background threads.

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

SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896
  • aha.. is there any way to do it ? – unbalanced Aug 14 '12 at 12:58
  • 1
    You could, however, create all the objects in parallel then come back and `AddRange` them to the UI in one go, right? If there's any performance benefit to be had, that is. – lc. Aug 14 '12 at 13:00
  • @lc. I see.. peformance is really important for me.I think the best way is using for :) thank you – unbalanced Aug 14 '12 at 13:03
3

I won't paraphrase other answers about threading, I'm just providing the fixed version of your second piece of 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);
        })));

but there won't be any benefit as explained by Coding Gorilla.

ken2k
  • 45,682
  • 8
  • 105
  • 160