Click here to Skip to main content
11,933,854 members (34,767 online)
Click here to Skip to main content
Add your own
alternative version


84 bookmarked

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.


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


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


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

QuestionSimply Clever Idea! Pin
Prabakaran T7-Jul-15 0:10
professionalPrabakaran T7-Jul-15 0:10 
QuestionVery cool! Pin
chris85020-Feb-15 11:14
memberchris85020-Feb-15 11:14 
QuestionCross-thread operation not valid error Pin
Member 460224719-Jan-15 5:48
memberMember 460224719-Jan-15 5:48 
QuestionStopping the proccess inside AccessControl Method Pin
Member 1114791718-Dec-14 5:34
memberMember 1114791718-Dec-14 5:34 
Generalthank u Pin
Member 1116174114-Nov-14 3:22
memberMember 1116174114-Nov-14 3:22 
Generalyou are awesome!!! Pin
jediYL2-Sep-14 20:07
memberjediYL2-Sep-14 20:07 
QuestionNo need to use Delagates Pin
Tino Fourie26-Jan-14 0:33
memberTino Fourie26-Jan-14 0:33 
GeneralMy vote of 5 Pin
thams13-Jul-13 9:24
memberthams13-Jul-13 9:24 
QuestionCross thread Pin
sabuni5-Jan-13 12:05
membersabuni5-Jan-13 12:05 
GeneralMy vote of 5 Pin
Disactive16-Dec-12 6:52
memberDisactive16-Dec-12 6:52 
QuestionNice Article Pin
Satish_Tijare21-Oct-12 20:57
memberSatish_Tijare21-Oct-12 20:57 
GeneralMy vote of 5 Pin
Osama El Shammari24-Jul-12 12:30
memberOsama El Shammari24-Jul-12 12:30 
GeneralYou Rock! Pin
TimmyTwins3-Jul-12 13:55
memberTimmyTwins3-Jul-12 13:55 
GeneralRe: You Rock! Pin
JoBrittain6-Jul-12 13:18
memberJoBrittain6-Jul-12 13:18 
QuestionMy vote Pin
kmf8-Dec-11 0:10
memberkmf8-Dec-11 0:10 
GeneralMy vote of 5 Pin
gafoor123-Nov-11 20:55
membergafoor123-Nov-11 20:55 
QuestionAlternative Method Pin
jlopez78814-Oct-11 9:24
memberjlopez78814-Oct-11 9:24 
GeneralMy vote of 5 Pin
Sunasara Imdadhusen9-Sep-11 4:19
memberSunasara Imdadhusen9-Sep-11 4:19 
GeneralMy vote of 5 Pin
Nanda_MR14-Mar-11 0:34
memberNanda_MR14-Mar-11 0:34 
GeneralI laughed a lot :) Pin
Member 102216227-Oct-10 0:17
memberMember 102216227-Oct-10 0:17 
GeneralYou missed the point. Pin
Elagizy27-Oct-10 5:14
memberElagizy27-Oct-10 5:14 
GeneralMy vote of 5 Pin
L3CodeProject22-Oct-10 11:47
memberL3CodeProject22-Oct-10 11:47 
Who would have thought. Thanks for publishing a resolution to thread update for a DataGrid.DataSource refresh problem.
GeneralSimply Great!! Pin
rahu_12221-Sep-10 21:37
memberrahu_12221-Sep-10 21:37 
Generalthanks :) :) Pin
ArunJoseph 471639512-Sep-10 6:30
memberArunJoseph 471639512-Sep-10 6:30 
GeneralThank You Pin
jb.jigar30-Jul-10 9:39
memberjb.jigar30-Jul-10 9:39 
GeneralMy vote of 5 Pin
jb.jigar30-Jul-10 9:11
memberjb.jigar30-Jul-10 9:11 
QuestionWhat if the method is a handler of a PropertyChanged Event from an updated class variable? Pin
bretddog1-Apr-10 1:01
memberbretddog1-Apr-10 1:01 
AnswerRe: What if the method is a handler of a PropertyChanged Event from an updated class variable? Pin
Elagizy1-Apr-10 8:53
memberElagizy1-Apr-10 8:53 
GeneralIf it doesn't work for anyone try this... Pin
Michaelsg23-Mar-10 0:14
memberMichaelsg23-Mar-10 0:14 
GeneralRe: If it doesn't work for anyone try this... Pin
Elagizy1-Apr-10 8:56
memberElagizy1-Apr-10 8:56 
GeneralWrap 'em up :) Pin
Derek R. White13-Mar-10 16:27
memberDerek R. White13-Mar-10 16:27 
GeneralThat was absolutely great Pin
chowdarysway15-Oct-09 23:34
memberchowdarysway15-Oct-09 23:34 
GeneralAccess the controls in Thread Pin
Muhammad Ghufran13-Aug-09 4:21
memberMuhammad Ghufran13-Aug-09 4:21 
JokeSimple and Clear !!! Pin
marcos_zanella15-Jul-09 19:29
membermarcos_zanella15-Jul-09 19:29 
GeneralThanks...This is very useful Pin
pmlchan18-Jun-09 23:58
memberpmlchan18-Jun-09 23:58 
GeneralMy vote of 2 Pin
Danilo Corallo8-May-09 3:11
memberDanilo Corallo8-May-09 3:11 
Generalthanks a lot for the tip Pin
wyn4429-Oct-08 7:54
memberwyn4429-Oct-08 7:54 
General[Message Removed] Pin
Mojtaba Vali10-May-08 23:22
memberMojtaba Vali10-May-08 23:22 
GeneralVery Good! Pin
tedpap8-Apr-08 21:18
membertedpap8-Apr-08 21:18 
GeneralA lot of thanx man Pin
Midhunlal G29-Nov-07 22:58
memberMidhunlal G29-Nov-07 22:58 
GeneralI love you man!!!! Pin
RanceDowner12349-Nov-07 13:07
memberRanceDowner12349-Nov-07 13:07 
GeneralExcellent! Pin
Bogdan Rechi10-Oct-07 11:09
memberBogdan Rechi10-Oct-07 11:09 
GeneralGreat thanks !!! Pin
iskenderun9-Jul-07 22:30
memberiskenderun9-Jul-07 22:30 
Questionwriting all the code in access control? [modified] Pin
Hari Om Prakash Sharma24-Jun-07 20:14
memberHari Om Prakash Sharma24-Jun-07 20:14 
AnswerRe: writing all the code in access control? Pin
elagizy24-Jun-07 21:57
memberelagizy24-Jun-07 21:57 
GeneralThanks again Pin
Elrond15-Jun-07 1:20
memberElrond15-Jun-07 1:20 
GeneralOutstanding! Pin
Fabio Videira8-Jun-07 11:10
memberFabio Videira8-Jun-07 11:10 
GeneralIt simply works :-) Pin
Diego F.24-May-07 5:58
memberDiego F.24-May-07 5:58 
Generalcross thread operation Pin
evergreen50115-May-07 5:07
memberevergreen50115-May-07 5:07 
Generalthis is easy yet brilliant Pin
sunlight2-May-07 18:46
membersunlight2-May-07 18:46 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 7 Oct 2006
Article Copyright 2006 by Elagizy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid