且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C# Windows 窗体中的线程错误

更新时间:2023-02-08 14:57:47

当您尝试从不是在其上创建的任何线程更新 UI 元素时,您会收到跨线程错误.

You get the Cross-thread error when you try to update a UI element from any thread it was not created on.

Windows 窗体中的控件绑定到特定线程并且不是线程安全的.因此,如果您从不同的线程调用控件的方法,则必须使用控件的调用方法之一将调用编组到正确的线程.此属性可用于确定是否必须调用 invoke 方法,如果您不知道哪个线程拥有控件,这会很有用.

Controls in Windows Forms are bound to a specific thread and are not thread safe. Therefore, if you are calling a control's method from a different thread, you must use one of the control's invoke methods to marshal the call to the proper thread. This property can be used to determine if you must call an invoke method, which can be useful if you do not know what thread owns a control.

请参阅此处了解更多信息

试试这个.这对我来说很好

Try this .This works fine for me

   if (pictureBoxname.InvokeRequired)
                    pictureBoxname.Invoke(new MethodInvoker(delegate
                    {
          //access picturebox here
                    }));
                else
        {

  //access picturebox here
}