Click here to Skip to main content
Click here to Skip to main content

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.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

QuestionVery cool! Pinmemberchris85020-Feb-15 11:14 
QuestionCross-thread operation not valid error PinmemberMember 460224719-Jan-15 5:48 
QuestionStopping the proccess inside AccessControl Method PinmemberMember 1114791718-Dec-14 5:34 
Generalthank u PinmemberMember 1116174114-Nov-14 3:22 
Generalyou are awesome!!! PinmemberjediYL2-Sep-14 20:07 
QuestionNo need to use Delagates PinmemberTino Fourie26-Jan-14 0:33 
GeneralMy vote of 5 Pinmemberthams13-Jul-13 9:24 
QuestionCross thread Pinmembersabuni5-Jan-13 12:05 
GeneralMy vote of 5 PinmemberDisactive16-Dec-12 6:52 
QuestionNice Article PinmemberSatish_Tijare21-Oct-12 20:57 
GeneralMy vote of 5 [modified] PinmemberOsama El Shammari24-Jul-12 12:30 
GeneralYou Rock! PinmemberTimmyTwins3-Jul-12 13:55 
GeneralRe: You Rock! PinmemberJoBrittain6-Jul-12 13:18 
QuestionMy vote Pinmemberkmf8-Dec-11 0:10 
GeneralMy vote of 5 Pinmembergafoor123-Nov-11 20:55 
QuestionAlternative Method Pinmemberjlopez78814-Oct-11 9:24 
GeneralMy vote of 5 PinmemberSunasara Imdadhusen9-Sep-11 4:19 
GeneralMy vote of 5 PinmemberNanda_MR14-Mar-11 0:34 
GeneralI laughed a lot :) PinmemberMember 102216227-Oct-10 0:17 
There are 2 problems in this "great and simple solution". First - sometimes Control.Invoke stucks, so calling thread is stuck inside the method. Some smart persons recommend to use BeginInvoke Laugh | :laugh: but it doesnt solve the problem, in some cases the "else" branch will not be called, (for example, when Message Queue is overheaded).
And the second Smile | :) )) When the control is INVISIBLE, its handle is destroyed, so InvokeRequired returns FALSE Smile | :) try to do the followin - create form, call Form.Hide() and then check what Form.InvokeRequired returns. You're gonna pleasantly surprised Smile | :) so you background thread will enter "else" branch and throws your favorite Exception.
GeneralYou missed the point. PinmemberElagizy27-Oct-10 5:14 
GeneralMy vote of 5 PinmemberL3CodeProject22-Oct-10 11:47 
GeneralSimply Great!! Pinmemberrahu_12221-Sep-10 21:37 
Generalthanks :) :) PinmemberArunJoseph 471639512-Sep-10 6:30 
GeneralThank You Pinmemberjb.jigar30-Jul-10 9:39 
GeneralMy vote of 5 Pinmemberjb.jigar30-Jul-10 9:11 

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.

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