Click here to Skip to main content
15,867,838 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

 
AnswerRe: writing all the code in access control? Pin
Elagizy24-Jun-07 20:57
Elagizy24-Jun-07 20:57 
GeneralThanks again Pin
Elrond15-Jun-07 0:20
Elrond15-Jun-07 0:20 
GeneralOutstanding! Pin
Fabio Videira8-Jun-07 10:10
Fabio Videira8-Jun-07 10:10 
GeneralIt simply works :-) Pin
Diego F.24-May-07 4:58
Diego F.24-May-07 4:58 
Generalcross thread operation Pin
evergreen50115-May-07 4:07
evergreen50115-May-07 4:07 
Generalthis is easy yet brilliant Pin
sunlight2-May-07 17:46
sunlight2-May-07 17:46 
QuestionPlease excuse my ignorance... Pin
HD-P26-Apr-07 10:38
HD-P26-Apr-07 10:38 
GeneralAny Solution for Same In .net 3.0 Pin
sumit_4palz24-Apr-07 23:04
sumit_4palz24-Apr-07 23:04 
GeneralRe: Any Solution for Same In .net 3.0 Pin
Elagizy25-Apr-07 3:00
Elagizy25-Apr-07 3:00 
AnswerRe: Any Solution for Same In .net 3.0 Pin
MaxtorM1236-Nov-08 5:50
MaxtorM1236-Nov-08 5:50 
QuestionHOW TO SOLVE: "Cross thread operation not valid" (DIRTY WAY) Pin
Miroslav Stampar12-Apr-07 4:16
Miroslav Stampar12-Apr-07 4:16 
GeneralClear, concise and works perfectly! Pin
VorTechS11-Apr-07 21:21
VorTechS11-Apr-07 21:21 
GeneralThx Pin
todwag4-Apr-07 4:27
todwag4-Apr-07 4:27 
GeneralWelcome all Pin
Elagizy17-Mar-07 21:08
Elagizy17-Mar-07 21:08 
GeneralThanks a lot! Pin
Raymond S.K.17-Mar-07 18:27
Raymond S.K.17-Mar-07 18:27 
GeneralThanks Pin
LOKIN SHAH5-Mar-07 15:35
LOKIN SHAH5-Mar-07 15:35 
GeneralThanks! Pin
Scott Bruhnsen15-Feb-07 8:52
Scott Bruhnsen15-Feb-07 8:52 
GeneralThanks!!! Pin
pirimoglu8-Feb-07 15:52
pirimoglu8-Feb-07 15:52 
GeneralThx - this solution rocks Pin
Andy3231-Jan-07 11:55
Andy3231-Jan-07 11:55 
GeneralVery nice Pin
toddacheson11-Jan-07 6:01
toddacheson11-Jan-07 6:01 
GeneralWorks in C# too [modified] Pin
iamstarbuck10-Dec-06 22:22
iamstarbuck10-Dec-06 22:22 
GeneralOMG, thank you so much Pin
Vinc__426-Oct-06 5:51
Vinc__426-Oct-06 5:51 
GeneralThnaks Pin
UnRusoDeCaracas25-Oct-06 9:36
UnRusoDeCaracas25-Oct-06 9:36 
GeneralHi Pin
Marco225020-Oct-06 12:45
Marco225020-Oct-06 12:45 
GeneralRe: Hi Pin
Elagizy20-Oct-06 21:05
Elagizy20-Oct-06 21:05 

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.