跨线程操作无效:控制... c#中的错误 [英] Cross-Thread operation not valid: Control... An error in c#

查看:66
本文介绍了跨线程操作无效:控制... c#中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我在这里有一个使用线程的类,但是当我运行代码时,出现此错误跨线程操作无效:控制...如何解决此问题?我有这段代码,如果数据库中的值更改为lock,则将ScreenSaver类设置为可见或已处置;当数据库中的数据设置为open时,将其设置为invisible.这是代码的示例

Hi guys i have a class here that make use of a thread but when i run the code i get this error Cross-Thread operation not valid: Control... how to solve this? i have this code that set the ScreenSaver Class to visible or disposed it if the value in the database changed to lock and set it to invisible when the data in the database is set to open... here is a sample of the code

    public partial class client : Form
    {
       public static bool locks= false;
       
        public client()
        {
            InitializeComponent();
            checkLock cl = new checkLock();
            Thread thread = new Thread(cl.checkLocks);
            thread.IsBackground = true;
            thread.Start();  
        }     
    }
    class checkLock
    {
        ScreenSaver s = new ScreenSaver();        
        public void checkLocks()
        {
            while(true){
            try
            {
               if (DBConnect.isExists("some checking in database..."))
                {
/* show the screensaver when the value in the database changes to open */
                    s.Show();                    
                }
                else
                {
/* Hide the screensaver when the value in the database changes to open */
                    s.Hide();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            Thread.Sleep(400);
            }
        }
    }
    
}



我希望您能提前帮助我...



i hope you can help me guys thanks in advance...

推荐答案

无法对任何UI对象的方法和属性进行跨线程调用.所有此类调用只能在UI线程中完成,对于System.Windows.Forms和WPF而言也是如此.如何使用不同线程的UI?您需要通过ControlDispatcher InvokeBeginInvoke方法使用线程间调用机制.

您可以在此处找到详细的说明和说明: Control.Invoke()与Control. BeginInvoke() [ ^ ].

—SA
Cross-thread call to any UI object''s methods and properties is impossible. All such calls can only be done in the UI thread, for System.Windows.Forms and WPF as well. How to work with UI from a different thread? You need to use inter-thread invocation mechanism via Control or Dispatcher Invoke or BeginInvoke methods.

You will find detailed explanation and instructions here: Control.Invoke() vs. Control.BeginInvoke()[^].

—SA





之所以出现此错误,是因为您试图在辅助线程上进行UI更改.

试试这个链接.

http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx [ ^ ]

您将需要进行检查.

这是所需的代码.

Hi,


You are getting this error because you are trying to make UI changes on a secondary thread.

Try this link.

http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx[^]

You will have to do a check.

here are the code required.

class checkLock
    {

        private delegate void ShowHideFormCallback(bool Show); //HERE

        ScreenSaver s = new ScreenSaver();
        public void checkLocks()
        {
            while(true){
            try
            {
               if (DBConnect.isExists("some checking in database..."))
                {
/* show the screensaver when the value in the database changes to open */
                   // s.Show();
                   ShowHideForm(true);//HERE
                }
                else
                {
/* Hide the screensaver when the value in the database changes to open */
                    //s.Hide();
                   ShowHideForm(false);//HERE
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            Thread.Sleep(400);
            }
        }

        //HERE
        private void ShowHideForm(bool Show)
        {
           if (s.InvokeRequired)
           {
              s.Invoke(new ShowHideFormCallback(ShowHideForm),Show);

           }
           else
           {
              if (Show)
              {
                s.Show();
              }
              else
              {
                s.Hide();
              }

           }
           
        }

    }




问候

特伦斯(Terence)




Regards

Terence



跨线程操作无效-当您尝试从未创建控件的线程访问控件时,通常会出现此错误.
我不确定checkLock中的控件,但似乎hide/show方法尝试修改对象s的某些内部属性(如果您注意的话,您会看到s是在主线程中创建的) ,然后从第二个线程访问该问题,有2种解决方案:1)您可以在调用checkLocks()方法的同一线程中创建checkLock对象,或者2)我更喜欢使用BeginInvoke()而不是创建新线程:
Hi,
Cross-thread operation not valid - it is an error which normally appears when you try to access a control from a thread which did not create the control.
I''m not sure about the controls from your checkLock but it seems that hide/show methods try to modify some internal properties of object s (if you pay attention you can see that s is created in main thread, and then is accessed from the second thread. There are 2 solutions for this problem: 1) you can create your checkLock object in the same thread which calls the checkLocks() method, or 2) I would prefer using BeginInvoke() instead of creating a new thread:
public partial class client : Form
    {
       public static bool locks= false;
       
        public client()
        {
            InitializeComponent();
            checkLock cl = new checkLock();
            BeginInvoke(new Action(()=>
                        {
                             cl.checkLocks();
                        }));
        }     
    }


有关BeginInvoke的更多信息- http://msdn.microsoft.com/en-us/library/0b1bf3y3.aspx [^ ]

问候


More about BeginInvoke - http://msdn.microsoft.com/en-us/library/0b1bf3y3.aspx[^]

Regards


这篇关于跨线程操作无效:控制... c#中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆