Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / Visual Basic
Article

How to solve "Cross thread operation not valid"

Rate me:
Please Sign up or sign in to vote.
4.82/5 (86 votes)
7 Oct 2006CPOL1 min read 482.7K   84   77
how to access a control from another thread which didn't create this control.
how to access a control from another thread which didn't create this control.

I faced this issue more than 1 time, I decided to collect info about it and made some changes on the code to simplify this problem to you cause it's really annoying and confusing to work with threading stuff. Here is a small code solves this problem FOREVER and in ANY case.

Error:

<quote> Cross thread operation not valid: Control "XXXXXXXXXX" accessed from a thread other than the thread it was created.

Solution:

1- Create your thread:

Private Strt As System.Threading.Thread


2- Start your thread wherever you want:

Strt = New System.Threading.Thread(AddressOf MyThread1)
Strt.Start()


3- Add the thread sub (MyThread1) and put whatever you want and remember that the lines which access a control from this thread will be separated to into another sub (the Delegate sub)

 Sub MyThread1
       ' Working code
       ' Working code
       ' Working code
       ' Working code
       ' Working code
       ' Working code

       AccessControl()

End Sub


From the previous code you will notice 2 things:
1st: AccessControl the sub which will be delegated.
2nd: ' Working code - which doesn't need a delegate to get it work. In other mean, it doesn't show up the error message you receive.

4- and finally, add the delegated sub:

Private Sub AccessControl()
        If Me.InvokeRequired Then
            Me.Invoke(New MethodInvoker(AddressOf AccessControl)) 
        Else
    ' Code wasn't working in the threading sub
    ' Code wasn't working in the threading sub
    ' Code wasn't working in the threading sub
    ' Code wasn't working in the threading sub
    ' Code wasn't working in the threading sub
            Button2.Visible = True
            Button3.Visible = True
            Opacity = 1
            ShowInTaskbar = True
        End If
    End Sub


From the previous code you will notice that all the codes which wasn't working in the threading sub will be added after "Else" line.
examples for some codes which needs to be delegated:
(Control).Visible
Me.Opacity
Me.ShowInTaskbar

I hope i simplified it enough, and now no worry about this issue again.
Special thanks to jmcilhinney for his help. You can also find this article at this link:
http://www.bitsnips.com/forums/viewtopic.php?p=102#102
Have a nice day :)

License

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


Written By
CEO InfraDrive
Egypt Egypt
Egyptian freelance programmer and the founder of InfraDrive, Inc.

Check:
https://infradrive.com
https://robomatic.ai
https://robomatic.chat
http://www.tufoxy.com

Comments and Discussions

 
GeneralMy vote of 5 Pin
Nanda_MR13-Mar-11 23:34
Nanda_MR13-Mar-11 23:34 
GeneralI laughed a lot :) Pin
Member 102216226-Oct-10 23:17
Member 102216226-Oct-10 23:17 
GeneralYou missed the point. Pin
Elagizy27-Oct-10 4:14
Elagizy27-Oct-10 4:14 
GeneralMy vote of 5 Pin
L3CodeProject22-Oct-10 10:47
L3CodeProject22-Oct-10 10:47 
GeneralSimply Great!! Pin
Rahul Chitte21-Sep-10 20:37
Rahul Chitte21-Sep-10 20:37 
Generalthanks :) :) Pin
ArunJoseph 471639512-Sep-10 5:30
ArunJoseph 471639512-Sep-10 5:30 
GeneralThank You Pin
jb.jigar30-Jul-10 8:39
jb.jigar30-Jul-10 8:39 
GeneralMy vote of 5 Pin
jb.jigar30-Jul-10 8:11
jb.jigar30-Jul-10 8:11 
QuestionWhat if the method is a handler of a PropertyChanged Event from an updated class variable? Pin
bretddog1-Apr-10 0:01
bretddog1-Apr-10 0:01 
AnswerRe: What if the method is a handler of a PropertyChanged Event from an updated class variable? Pin
Elagizy1-Apr-10 7:53
Elagizy1-Apr-10 7:53 
GeneralIf it doesn't work for anyone try this... Pin
Michaelsg22-Mar-10 23:14
Michaelsg22-Mar-10 23:14 
GeneralRe: If it doesn't work for anyone try this... Pin
Elagizy1-Apr-10 7:56
Elagizy1-Apr-10 7:56 
GeneralWrap 'em up :) Pin
Derek R. White13-Mar-10 15:27
Derek R. White13-Mar-10 15:27 
GeneralThat was absolutely great Pin
Vamsi Krishna Bandi15-Oct-09 22:34
Vamsi Krishna Bandi15-Oct-09 22:34 
GeneralAccess the controls in Thread Pin
Muhammad Ghufran13-Aug-09 3:21
Muhammad Ghufran13-Aug-09 3:21 
JokeSimple and Clear !!! Pin
marcos_zanella15-Jul-09 18:29
professionalmarcos_zanella15-Jul-09 18:29 
GeneralThanks...This is very useful Pin
pmlchan18-Jun-09 22:58
pmlchan18-Jun-09 22:58 
GeneralMy vote of 2 Pin
Danilo Corallo8-May-09 2:11
Danilo Corallo8-May-09 2:11 
Generalthanks a lot for the tip Pin
wyn4429-Oct-08 6:54
wyn4429-Oct-08 6:54 
GeneralSimple and good but defeats the purpose of multi-threading Pin
tinku00716-Aug-08 3:50
tinku00716-Aug-08 3:50 
GeneralRe: Simple and good but defeats the purpose of multi-threading Pin
Elagizy16-Aug-08 8:11
Elagizy16-Aug-08 8:11 
GeneralGood Pin
cpearl_s8-Aug-08 16:51
cpearl_s8-Aug-08 16:51 
General[Message Removed] Pin
Mojtaba Vali10-May-08 22:22
Mojtaba Vali10-May-08 22:22 
GeneralVery Good! Pin
tedpap8-Apr-08 20:18
tedpap8-Apr-08 20:18 
GeneralA lot of thanx man Pin
Midhunlal G29-Nov-07 21:58
Midhunlal G29-Nov-07 21:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.