Click here to Skip to main content
Licence CPOL
First Posted 7 Oct 2006
Views 174,667
Bookmarked 77 times

How to solve "Cross thread operation not valid"

By | 7 Oct 2006 | Article
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:

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)

About the Author

Elagizy

Software Developer (Senior)
InfraDrive
Egypt Egypt

Member

Egyptian freelance programmer and the founder of InfraDrive, Inc.
 
Check:
http://www.infradrive.com
http://www.bitsnips.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionMy vote Pinmemberkmf23:10 7 Dec '11  
GeneralMy vote of 5 Pinmembergafoor119:55 23 Nov '11  
QuestionAlternative Method Pinmemberjlopez7888:24 14 Oct '11  
GeneralMy vote of 5 PinmemberSunasara Imdadhusen3:19 9 Sep '11  
GeneralMy vote of 5 PinmemberNanda_MR23:34 13 Mar '11  
GeneralI laughed a lot :) PinmemberMember 102216223:17 26 Oct '10  
GeneralYou missed the point. PinmemberElagizy4:14 27 Oct '10  
GeneralMy vote of 5 PinmemberL3CodeProject10:47 22 Oct '10  
GeneralSimply Great!! Pinmemberrahu_12220:37 21 Sep '10  
Generalthanks :) :) PinmemberArunJoseph 47163955:30 12 Sep '10  
GeneralThank You Pinmemberjb.jigar8:39 30 Jul '10  
GeneralMy vote of 5 Pinmemberjb.jigar8:11 30 Jul '10  
QuestionWhat if the method is a handler of a PropertyChanged Event from an updated class variable? Pinmemberbretddog0:01 1 Apr '10  
AnswerRe: What if the method is a handler of a PropertyChanged Event from an updated class variable? PinmemberElagizy7:53 1 Apr '10  
GeneralIf it doesn't work for anyone try this... PinmemberMichaelsg23:14 22 Mar '10  
GeneralRe: If it doesn't work for anyone try this... PinmemberElagizy7:56 1 Apr '10  
GeneralWrap 'em up :) PinmemberDerek R. White15:27 13 Mar '10  
GeneralThat was absolutely great Pinmemberchowdarysway22:34 15 Oct '09  
GeneralAccess the controls in Thread PinmemberMuhammad Ghufran3:21 13 Aug '09  
JokeSimple and Clear !!! Pinmembermarcos_zanella18:29 15 Jul '09  
GeneralThanks...This is very useful Pinmemberpmlchan22:58 18 Jun '09  
GeneralMy vote of 2 PinmemberDanilo Corallo2:11 8 May '09  
Generalthanks a lot for the tip Pinmemberwyn446:54 29 Oct '08  
GeneralSimple and good but defeats the purpose of multi-threading Pinmembertinku0073:50 16 Aug '08  
GeneralRe: Simple and good but defeats the purpose of multi-threading PinmemberElagizy8:11 16 Aug '08  
It depends on what you are trying to achieve, may be your application depends heavily on running many threads, however I never found this method slows my applications. As long as both solutions works, choose the faster performance.
 
Best Regards,
Ehab El-agizy

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 7 Oct 2006
Article Copyright 2006 by Elagizy
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid