Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Parallel.ForEach重载_C# - Fatal编程技术网

C# Parallel.ForEach重载

C# Parallel.ForEach重载,c#,C#,我试图做的是让每个线程都有一个本地位图变量,并在主体完成执行后处理它,我尝试了以下方法 Parallel.ForEach<Models.Record, Bitmap> (RecordsToBeProcessed, new ParallelOptions() { MaxDegreeOfParallelism = coreCount * 2 }, (bitmap) =>

我试图做的是让每个线程都有一个本地位图变量,并在主体完成执行后处理它,我尝试了以下方法

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();
                });
Parallel.ForEach
(已处理的记录,
新的ParallelOptions(){MaxDegreeOfParallelism=coreCount*2},
(位图)=>
{
//初始化位图变量
锁定(源位图)
{
bitmap=SourceBitmap.Clone();
}
},
(记录,位图)=>
{
//身体
}
,
(位图)=>
{//最后处理本地线程位图变量
Dispose();
});
对于第三个参数,应该是我初始化局部变量的地方,我得到的是,它不需要一个参数,但我认为我做得不对,我似乎找不到正确的重载

我想做的是

  • 传递一个IEnumarable源,该源是
    RecordsToBeProcessed
  • 具有一个局部变量,该局部变量仅在程序开始时初始化一次 位图类型的线程
  • 在正文中访问局部位图变量和记录
  • 最后处理位图

  • 不存在
    ForEach
    的重载,该重载具有不在
    ParallelLoopState
    state对象中传递的初始和最终线程本地操作。您只需要在主循环体中再添加一个参数。您还必须
    返回位于身体末端的对象,以便将其传递给下一次迭代以重复使用。最后,在线程初始化器中,当您真正想做的是对在该函数中创建的位图执行
    返回时,您将传入
    位图
    变量

    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();
                });
    
    Parallel.ForEach
    (已处理的记录,
    新的ParallelOptions(){MaxDegreeOfParallelism=coreCount*2},
    ()=>//此处未传递任何对象,我们要重新运行一个新位图,而不是传递一个。
    {
    //初始化位图变量
    锁定(源位图)
    {
    return SourceBitmap.Clone();//返回为线程本地使用而创建的对象。
    }
    },
    (record,loopState,bitmap)=>//loopState是新的,但您根本不需要在主体中使用变量。
    {
    //身体
    return bitmap;//返回是新的,您在这里传递的对象将是下一次使用相同线程(或传递到最终函数)的输入对象
    }
    ,
    (位图)=>
    {//最后处理本地线程位图变量
    Dispose();
    });
    
    我在上面的例子中使用