Click here to Skip to main content
Licence CPOL
First Posted 7 Oct 2006
Views 161,180
Bookmarked 76 times

How to solve "Cross thread operation not valid"

By Elagizy | 7 Oct 2006
how to access a control from another thread which didn't create this control.
4 votes, 5.9%
1
3 votes, 4.4%
2
3 votes, 4.4%
3
4 votes, 5.9%
4
54 votes, 79.4%
5
4.79/5 - 68 votes
7 removed
μ 4.48, σa 2.03 [?]
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 Pinmemberkmf0:10 8 Dec '11  
GeneralMy vote of 5 Pinmembergafoor120:55 23 Nov '11  
QuestionAlternative Method Pinmemberjlopez7889:24 14 Oct '11  
GeneralMy vote of 5 PinmemberSunasara Imdadhusen4:19 9 Sep '11  
GeneralMy vote of 5 PinmemberNanda_MR0:34 14 Mar '11  
GeneralI laughed a lot :) PinmemberMember 10221620:17 27 Oct '10  
GeneralYou missed the point. PinmemberElagizy5:14 27 Oct '10  
GeneralMy vote of 5 PinmemberL3CodeProject11:47 22 Oct '10  
GeneralSimply Great!! Pinmemberrahu_12221:37 21 Sep '10  
Generalthanks :) :) PinmemberArunJoseph 47163956:30 12 Sep '10  
GeneralThank You Pinmemberjb.jigar9:39 30 Jul '10  
GeneralMy vote of 5 Pinmemberjb.jigar9:11 30 Jul '10  
QuestionWhat if the method is a handler of a PropertyChanged Event from an updated class variable? Pinmemberbretddog1:01 1 Apr '10  
AnswerRe: What if the method is a handler of a PropertyChanged Event from an updated class variable? PinmemberElagizy8:53 1 Apr '10  
GeneralIf it doesn't work for anyone try this... PinmemberMichaelsg0:14 23 Mar '10  
GeneralRe: If it doesn't work for anyone try this... PinmemberElagizy8:56 1 Apr '10  
GeneralWrap 'em up :) PinmemberDerek R. White16:27 13 Mar '10  
GeneralThat was absolutely great Pinmemberchowdarysway23:34 15 Oct '09  
GeneralAccess the controls in Thread PinmemberMuhammad Ghufran4:21 13 Aug '09  
JokeSimple and Clear !!! Pinmembermarcos_zanella19:29 15 Jul '09  
GeneralThanks...This is very useful Pinmemberpmlchan23:58 18 Jun '09  
GeneralMy vote of 2 PinmemberDanilo Corallo3:11 8 May '09  
Generalthanks a lot for the tip Pinmemberwyn447:54 29 Oct '08  
GeneralSimple and good but defeats the purpose of multi-threading Pinmembertinku0074:50 16 Aug '08  
GeneralRe: Simple and good but defeats the purpose of multi-threading PinmemberElagizy9:11 16 Aug '08  

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
Web03 | 2.5.120209.1 | Last Updated 7 Oct 2006
Article Copyright 2006 by Elagizy
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid