Cross-thread operation not valid – error solved in C#

Author posted by Jitendra on Posted on under category Categories c#, Tech Tips and tagged as Tags with Leave a comment on Cross-thread operation not valid – error solved in C#

Cross-thread operation not valid – error solved in C#

For the┬ábeginners, working on thread might come across the error saying “Cross-thread operation not valid“. This type error comes normally when Windows control created in one thread and accessed in another.
Because the Windows control are not thread safe by default.

ThreadCommunication
Thread Communication

The best way to solve this kind of error is :

if (InvokeRequired)
                {
                    this.Invoke(new MethodInvoker(delegate
                    {
                       //Your code here, like set text box content or get text box contents etc..
                    }));
                }
                else
                {
                     // Your code here, like set text box content or get text box contents etc..
                     // SAME CODE AS ABOVE
                }

Related posts