Matimont
Matimont

Reputation: 759

Parallel for and Progressbar

I have this code:

        Parallel.For(0, img.Count(), i =>
        {
            img[i].Scale = escala_axial;
            Bitmap tmp_b = new Bitmap((System.Drawing.Image)img[i].RenderImage(0));
            tmp_b = filtro.Apply(tmp_b);
            imagenes[i] = tmp_b;
            Progress_Bar_Loading_Images.Invoke((MethodInvoker)(delegate() { Progress_Bar_Loading_Images.Value++; }));
        });

It works fine without the ProgressBar invoke thing. Now, when I use the Invoke, then it looks like it never ends the loop and the screen freezes. Any ideas what may be the issue?

Thanks!

EDIT:

Here's the answer: Thanks for the feedback!

        Parallel.For(0, img.Count(), i =>
        {
            img[i].Scale = escala_axial;
            Bitmap tmp_b = new Bitmap((System.Drawing.Image)img[i].RenderImage(0));
            tmp_b = filtro.Apply(tmp_b);
            imagenes[i] = tmp_b;
            this.Invoke(new Action(() => Progress_Bar_Loading_Images.Value++));
        });

Upvotes: 1

Views: 1712

Answers (1)

terrybozzio
terrybozzio

Reputation: 4542

Instead of progressbar.Invoke() use this:

//rest of loop....
this.Invoke(/*your code here*/);

Upvotes: 2

Related Questions