【发布时间】:2012-07-12 17:52:56
【问题描述】:
我有一个计时器来每次验证一个条件,如果条件得到验证,只显示一次弹出表单。我想并行验证所有实例,所以我使用了parallel.for,但我有这个错误“跨线程操作无效:控制'CameraViewVS'从创建它的线程以外的线程访问。”在“frm.WindowState = FormWindowState.Normal;”行中
这是我的代码:
public void timer1_Tick(object source, EventArgs e)
{
Parallel.For(0, nbre, l =>
{
cameraInstanceList[l].Start();
if (cameraInstanceList[l].MoveDetection == true)
{
//show the the form S once
foreach (Form S in Application.OpenForms)
{
var frm = S as Formes.CameraViewVS;
if (frm != null && frm.IP == cameraInstanceList[l].adresse)
{
cameraInstanceList[l].MoveDetection = false;
frm.WindowState = FormWindowState.Normal;
frm.Activate();
return;
}
}
f1 = new Formes.CameraViewVS(cameraInstanceList[l],
adresseIPArray[l]);
f1.Show(this);
}
}
);
【问题讨论】:
标签: multithreading c#-4.0