Multithreading 错误跨线程操作无效:控件';CameraViewVS';从创建它的线程以外的线程访问。平行
我有一个计时器,每次验证一个条件,如果条件得到验证,则只显示一次弹出窗体。我想并行验证所有实例,所以我使用parallel.for,但我有一个错误“跨线程操作无效:从创建控件的线程以外的线程访问控件'CameraViewVS'。行中“frm.WindowState=FormWindowState.Normal;” 这是我的代码:Multithreading 错误跨线程操作无效:控件';CameraViewVS';从创建它的线程以外的线程访问。平行,multithreading,c#-4.0,Multithreading,C# 4.0,我有一个计时器,每次验证一个条件,如果条件得到验证,则只显示一次弹出窗体。我想并行验证所有实例,所以我使用parallel.for,但我有一个错误“跨线程操作无效:从创建控件的线程以外的线程访问控件'CameraViewVS'。行中“frm.WindowState=FormWindowState.Normal;” 这是我的代码: public void timer1_Tick(object source, EventArgs e) { Parallel.Fo
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);
}
}
);
WinForm对象实例上的大多数属性都需要从创建它们的线程访问。您可以使用Control.InvokeRequired属性来确定是否需要使用Control(或form)Invoke方法在UI线程上执行代码 在主UI线程上而不是在任何线程池线程上创建大多数WinForm控件也是一种很好的做法。在WinForms应用程序中,您可以使用
SynchronizationContext
确保在UI线程上调用某些代码,例如创建表单
编辑:已更改,因此检测到移动后该方法不会返回
public void timer1_Tick(object source, EventArgs e)
{
// assume this is being called on the UI thread, and save the thread synchronization context
var uiContext = SynchronizationContext.Current;
Parallel.For(0, nbre, l =>
{
while (true)
{
Thread.Sleep(250); // <--- sleep for 250 ms to avoid "busy" wait
cameraInstanceList[l].Start();
if (cameraInstanceList[l].MoveDetection == true)
{
// capture instances used in closures below
var cameraInstance = cameraInstanceList[l];
var ipAdresse = adresseIPArray[l];
//show the the form S once
foreach (Form S in Application.OpenForms)
{
var frm = S as Formes.CameraViewVS;
if (frm != null)
{
// create delegate to be invoked on form's UI thread.
var action = new Action(() =>
{
if (frm.IP == cameraInstance.adresse)
{
cameraInstance.MoveDetection = false;
frm.WindowState = FormWindowState.Normal;
frm.Activate();
}
};
if (frm.InvokeRequired)
frm.Invoke(action);
else
action();
continue; // <--- go back to the top of the while loop
// and wait for next detection
}
}
// create delegate to create new form on UI thread.
var createNewFormCallback = new SendOrPostCallback((o) =>
{
f1 = new Formes.CameraViewVS(cameraInstance, ipAdresse);
f1.Show(this);
};
// and invoke the delegate on the ui thread
uiContext.Send(createNewFormCallback, null);
}
}
}
);
}
public void timer1\u勾选(对象源,事件参数e)
{
//假设这是在UI线程上调用的,并保存线程同步上下文
var uiContext=SynchronizationContext.Current;
平行。对于(0,nbre,l=>
{
while(true)
{
Thread.Sleep(250);//Thomas非常接近正确答案,因为每个控件都在不同的线程中运行。您只需编写一个代码,用于控件使用的资源的上下文切换
线程..不要担心,在c sharp中,您有很多工具可以实现这一点。只需使用BeginInvoke和Invoke,我希望您能够解决您的问题。编写此代码来代替旧代码块
var action = new Action(() =>
{
if (frm.IP == cameraInstance.adresse)
{
cameraInstance.MoveDetection = false;
frm.WindowState = FormWindowState.Normal;
frm.Activate();
}
};
if (frm.InvokeRequired)
frm.BeginInvoke(action);
else
frm.Invoke(action);
它在uiContext.Send(createNewFormCallback,xxx)中缺少一个参数,我添加了“line”+l.ToString(),但当我执行它时,只显示一个弹出窗口,我在使用线程方面比较新,所以如果你能给我一些好教程的链接,谢谢again@Ostorlabi感谢您指出这一点。第二个参数应该是null
。我建议您使用调试器在MovedDetection==true
之后放置断点,或者使用Debug、 WriteLine记录执行流以查看哪些工作正常,哪些不正常。我添加了Debug.WriteLine(“错误”);after MovedDetection==true,要记录流,但我看不到任何东西,是否保存了文件?@Ostorlabi如果您在使用Visual Studio调试器中运行此文件,您应该能够在输出窗口中看到它。请参阅和独立调试视图@Thomas:谢谢,我看到了输出窗口,它显示消息“error”只有在第一次有move的时候,即使movedDetection==true用于另一个实例或同一个相关实例,它也不会在其他时间显示“error”。然而,当我在没有并行的情况下使用初始代码进行测试时,它会显示“error”每次都有一个移动。我写道,这个,它可以显示多个弹出窗口,但问题是它们与移动不同步,我解释说,当一个摄像头中有一个移动时,它会显示出来,但如果在第一个摄像头的同时第二个和第三个摄像头有移动,第二个和第三个摄像头不会显示,直到我关闭f但是如果我不使用parallel.for(只是简单的boucle for),如果您看到我的第一个代码,它工作得很好,并且比parallel.for快。