FPGA
FPGA

Reputation: 3855

Parallel.ForEach overloads

i am trying to do is having a local Bitmap variabe per thread and dispose it after the body finishes executing, i tried the following

Parallel.ForEach<Models.Record, Bitmap>
                (RecordsToBeProcessed,
                new ParallelOptions() { MaxDegreeOfParallelism = coreCount * 2 },
                (bitmap) =>
                {
                    //initalize the bitmap variable
                    lock (SourceBitmap)
                    {
                        bitmap = SourceBitmap.Clone();
                    }
                },
                (record, bitmap) =>
                {
                    //the body 
                }
                ,
                (bitmap) =>
                {  //finally dispose the local thread bitmap variable
                    bitmap.Dispose();
                });

for the third parameter which is supposed to be where i initialize the local variable i am getting that it does not take one argument, but i think i am doing it wrong and i cant seem to find the right overload.

what i am trying to do is

  1. pass an IEnumarable Source which is RecordsToBeProcessed
  2. have a local variable that is initalized only once at the start of the thread of type Bitmap.
  3. in the body access to the local bitmap variable and the record
  4. in the end dispose the Bitmap

Upvotes: 1

Views: 710

Answers (1)

Scott Chamberlain
Scott Chamberlain

Reputation: 127543

There is no overload of ForEach which has the initial and final thread local operations that does not pass in a ParallelLoopState state object. You just need to add in one more argument to your main loop body. Also you must return the object at the end of your body so it may be passed along to the next iteration to be re-used. Lastly in your thread initizer you where passing in a bitmap variable when what you really want to be doing is doing a return on the bitmap you create in that function.

Parallel.ForEach<Models.Record, Bitmap>
            (RecordsToBeProcessed,
            new ParallelOptions() { MaxDegreeOfParallelism = coreCount * 2 },
            () => //No object is passed in here, we want to retun a new Bitmap, not pass in one.
            {
                //initalize the bitmap variable
                lock (Sourcebitmap)
                {
                    return SourceBitmap.Clone(); //You return the object you created for the thread local use.
                }
            },
            (record, loopState, bitmap) => //loopState is new, but you don't need to use the variable in the body at all.
            {
                //the body

                return bitmap; //The return is new, the object you pass in here will be the input object on the next itteration that uses the same thread (or be passed to the final function)
            }
            ,
            (bitmap) =>
            {  //finally dispose the local thread bitmap variable
                bitmap.Dispose();
            });

I am using this overload in the above example

Upvotes: 2

Related Questions