Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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...
Posted
Updated 5-Apr-11 20:49pm
v2

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
 
Share this answer
 
Comments
Kim Togo 6-Apr-11 2:41am    
Yes, that is the way :-). My 5.
Sergey Alexandrovich Kryukov 6-Apr-11 2:42am    
Thank you, Kim.
--SA
CPallini 6-Apr-11 3:01am    
Correct, as always. My 5.
Sergey Alexandrovich Kryukov 6-Apr-11 3:22am    
Thank you very much.
--SA
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.

C#
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();
              }

           }
           
        }

    }



Regards

Terence
 
Share this answer
 
v2
Comments
Kim Togo 6-Apr-11 2:49am    
This will work.
Madzmar25 6-Apr-11 2:56am    
I love your answer switchersoft... it works i had this line of code

client.s.Invoke((MethodInvoker)delegate
{
client.locks = false;
client.s.Hide();
});


it work but as soon as the value in the database changes the class screensaver just freezes... your solution works really well thanks...
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();
                        }));
        }     
    }

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

Regards
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900