Click here to Skip to main content
12,949,700 members (73,658 online)
Click here to Skip to main content
Add your own
alternative version


84 bookmarked
Posted 7 Oct 2006

How to solve "Cross thread operation not valid"

, 7 Oct 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
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.


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


1- Create your thread:

Private Strt As System.Threading.Thread

2- Start your thread wherever you want:

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

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


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)) 
    ' 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:

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:
Have a nice day :)


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


About the Author

Software Developer (Senior) InfraDrive
Egypt Egypt
Egyptian freelance programmer and the founder of InfraDrive, Inc.


You may also be interested in...


Comments and Discussions

GeneralThat was absolutely great Pin
chowdarysway15-Oct-09 22:34
memberchowdarysway15-Oct-09 22:34 
GeneralAccess the controls in Thread Pin
Muhammad Ghufran13-Aug-09 3:21
memberMuhammad Ghufran13-Aug-09 3:21 
JokeSimple and Clear !!! Pin
marcos_zanella15-Jul-09 18:29
membermarcos_zanella15-Jul-09 18:29 
GeneralThanks...This is very useful Pin
pmlchan18-Jun-09 22:58
memberpmlchan18-Jun-09 22:58 
GeneralMy vote of 2 Pin
Danilo Corallo8-May-09 2:11
memberDanilo Corallo8-May-09 2:11 
Generalthanks a lot for the tip Pin
wyn4429-Oct-08 6:54
memberwyn4429-Oct-08 6:54 
General[Message Removed] Pin
Mojtaba Vali10-May-08 22:22
memberMojtaba Vali10-May-08 22:22 
GeneralVery Good! Pin
tedpap8-Apr-08 20:18
membertedpap8-Apr-08 20:18 
Nice and simple! Big Grin | :-D
GeneralA lot of thanx man Pin
Midhunlal G29-Nov-07 21:58
memberMidhunlal G29-Nov-07 21:58 
GeneralI love you man!!!! Pin
RanceDowner12349-Nov-07 12:07
memberRanceDowner12349-Nov-07 12:07 
GeneralExcellent! Pin
Bogdan Rechi10-Oct-07 10:09
memberBogdan Rechi10-Oct-07 10:09 
GeneralGreat thanks !!! Pin
iskenderun9-Jul-07 21:30
memberiskenderun9-Jul-07 21:30 
Questionwriting all the code in access control? [modified] Pin
Hari Om Prakash Sharma24-Jun-07 19:14
memberHari Om Prakash Sharma24-Jun-07 19:14 
AnswerRe: writing all the code in access control? Pin
elagizy24-Jun-07 20:57
memberelagizy24-Jun-07 20:57 
GeneralThanks again Pin
Elrond15-Jun-07 0:20
memberElrond15-Jun-07 0:20 
GeneralOutstanding! Pin
Fabio Videira8-Jun-07 10:10
memberFabio Videira8-Jun-07 10:10 
GeneralIt simply works :-) Pin
Diego F.24-May-07 4:58
memberDiego F.24-May-07 4:58 
Generalcross thread operation Pin
evergreen50115-May-07 4:07
memberevergreen50115-May-07 4:07 
Generalthis is easy yet brilliant Pin
sunlight2-May-07 17:46
membersunlight2-May-07 17:46 
QuestionPlease excuse my ignorance... Pin
HD-P26-Apr-07 10:38
memberHD-P26-Apr-07 10:38 
GeneralAny Solution for Same In .net 3.0 Pin
sumit_4palz24-Apr-07 23:04
membersumit_4palz24-Apr-07 23:04 
GeneralRe: Any Solution for Same In .net 3.0 Pin
elagizy25-Apr-07 3:00
memberelagizy25-Apr-07 3:00 
AnswerRe: Any Solution for Same In .net 3.0 Pin
MaxtorM1236-Nov-08 5:50
memberMaxtorM1236-Nov-08 5:50 
QuestionHOW TO SOLVE: "Cross thread operation not valid" (DIRTY WAY) Pin
Miroslav Stampar12-Apr-07 4:16
memberMiroslav Stampar12-Apr-07 4:16 
GeneralClear, concise and works perfectly! Pin
VorTechS11-Apr-07 21:21
memberVorTechS11-Apr-07 21:21 

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.

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