0

I have a timer to verify one condition every time and show pop up form only once if the condition is verified. I want to verify in parallel all instances, so i used parallel.for, but i have this error "Cross-thread operation not valid: Control 'CameraViewVS' accessed from a thread other than the thread it was created on." in line " frm.WindowState = FormWindowState.Normal;"

this is my code:

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);

            }

        }               
        );
4

2 回答 2

0

托马斯非常接近正确的答案,因为每个控件都在不同的线程中运行。您应该只编写一个代码用于控件线程正在使用的资源的上下文切换..不要担心您有很多工具可以做到这一点在 c 中。只需使用 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);
于 2012-07-14T21:18:38.113 回答
0

WinForm 对象实例上的大多数属性都需要从创建它们的线程中访问。您可以使用 Control.InvokeRequired 属性来确定是否需要使用控件(或表单)的 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); 
                }            
            } 
        }
        );
}
于 2012-07-12T22:17:21.423 回答