[英]Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on in window form
[英]ParallelFor | Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on
我有以下要尝试并行化的简单代码:
Parallel.For (0,countimages, i =>
{
picbox[i] = new PictureBox();
picbox[i].Image = image;
TableLayoutPanel1.Controls.Add(picbox[i], column, row);
column+=1;
});
我得到这个错误:
ParallelFor | 跨线程操作无效:控制TableLayoutPanel1从创建该线程的线程之外的其他线程访问。
我究竟做错了什么?
只能从创建UI元素的线程进行UI更改。 您的代码在多个线程中运行,并且每个线程都试图更改TableLayoutPanel1.Controls集合。
您必须封送回UI线程的调用。
您也可能会遇到与列值有关的意外行为,因为您似乎正在尝试在线程之间共享它。 可能会有一些控件尝试在该值增加之前添加到同一列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.