Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.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# picbox.BorderStyle=BorderStyle.FixedSingle |交叉线程操作无效_C#_Multithreading_Winforms_Parallel Processing - Fatal编程技术网

C# picbox.BorderStyle=BorderStyle.FixedSingle |交叉线程操作无效

C# picbox.BorderStyle=BorderStyle.FixedSingle |交叉线程操作无效,c#,multithreading,winforms,parallel-processing,C#,Multithreading,Winforms,Parallel Processing,我正在从事一个C#Winforms项目,该项目在网格中加载图像,我实现了并行性和线程来学习一些关于它的知识,但有些东西我搞不清楚。我需要在运行时更改一组PictureBox的边框样式,以下是我的代码: Task.Factory.StartNew(() => { Parallel.For(0, img.Count, i => { Bitmap tmp_b = new Bitmap((System.Drawing.Image

我正在从事一个C#Winforms项目,该项目在网格中加载图像,我实现了并行性和线程来学习一些关于它的知识,但有些东西我搞不清楚。我需要在运行时更改一组PictureBox的边框样式,以下是我的代码:

Task.Factory.StartNew(() =>
    {
        Parallel.For(0, img.Count, i =>
        {
            Bitmap tmp_b = new Bitmap((System.Drawing.Image)img[i].RenderImage(0));
            imagenes[i] = tmp_b;
            Progress_Bar_Loading_Images.Invoke((Action)delegate { reportarprogreso(); });
        });
        for (int i = inicio_set; i < final_set; ++i)
        {
            picbox[i].Image = imagenes[i];
            picbox[i].BorderStyle = BorderStyle.FixedSingle;
        }
    });
Task.Factory.StartNew(()=>
{
Parallel.For(0,img.Count,i=>
{
位图tmp_b=新位图((System.Drawing.Image)img[i].渲染(0));
imagenes[i]=tmp_b;
进度条加载图像。调用((操作)委托{reportarprogreso();});
});
对于(int i=inicio_集;i
问题出在这一行:
picbox[i]。BorderStyle=BorderStyle.FixedSingle我收到一条错误消息,提示:控件“”是从创建它的线程以外的线程访问的

我想我知道为什么会发生这种情况,但我不太确定如何解决它。我尝试将(int I=inicio_set;I
放在任务之外,但是图像不会被分配到pictureboxes,因为它是一个不同的线程(main)在任务完成之前,图像是不可用的,所以我想我需要像更新ProgressBar一样更新BorderStyle,但不确定如何更新

谢谢,


Matias。

在不同的线程中执行
for
循环

主体的
更改为:

if (picbox[i].InvokeRequired)
{
    picbox[i].Invoke(new Action(() =>
    {
        picbox[i].BorderStyle = BorderStyle.FixedSingle;
    }));
}
else
{
      picbox[i].BorderStyle = BorderStyle.FixedSingle;
}
或者简单地在任务上使用
Async
-
wait
;使调用方方法
异步
和任务:

await Task.Factory.StartNew(...)