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
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

QuestionNo need to use Delagates PinmemberTino Fourie25-Jan-14 23:33 
Although this article is very old, most of the content is still applicable.
I experienced Cross-thread issues when I implemented the FileSystemWatcher class native to System.IO namespace in stead of the old conventional way of using timers (I did use a timer but found the SystemFileWatcher class beats timers hands down).
The first problem I experience was with a DataGridView. Searches brought me to start using Delegates to update user controls on another thread - needless to say this was way over my head at the time.
Implementing the Delegate process on the DataGridView solved my problem.
The next challenge was to insert an image into a ListView. The first iteration using the Delegate method went flawless BUT failed on the second iteration and the application terminated.
DO NOT implement the following in your code
"System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False"
Sooner or later you will regret this.
However, I kept searching and found the most simple and perfectly stable solution without using Delegates or disabling Cross-Thread checking.
In the Sub where you declare your SystemFileWatcher add the following line of code:
Private Sub StartFileWatcher()
        ' Create a new FileSystemWatcher and set its properties. 
        Dim FileWatcher As New FileSystemWatcher()
        FileWatcher.SynchronizingObject = Me
        FileWatcher.Path = txtImageFolder.Text
        ' Watch for changes in LastWrite times
        FileWatcher.NotifyFilter = (NotifyFilters.LastAccess)
        ' Add event handlers.
        AddHandler FileWatcher.Changed, AddressOf OnChanged
        ' Begin watching.
        FileWatcher.EnableRaisingEvents = True

    End Sub
FileWatcher.SynchronizingObject = Me allowed me to code as if there was no background thread running.
For more information please refer to this article by Richard Carr:[^]
GeneralMy vote of 5 Pinmemberthams13-Jul-13 8:24 
QuestionCross thread Pinmembersabuni5-Jan-13 11:05 
GeneralMy vote of 5 PinmemberDisactive16-Dec-12 5:52 
QuestionNice Article PinmemberSatish_Tijare21-Oct-12 19:57 
GeneralMy vote of 5 [modified] PinmemberOsama El Shammari24-Jul-12 11:30 
GeneralYou Rock! PinmemberTimmyTwins3-Jul-12 12:55 
GeneralRe: You Rock! PinmemberJoBrittain6-Jul-12 12:18 
QuestionMy vote Pinmemberkmf7-Dec-11 23:10 
GeneralMy vote of 5 Pinmembergafoor123-Nov-11 19:55 
QuestionAlternative Method Pinmemberjlopez78814-Oct-11 8:24 
GeneralMy vote of 5 PinmemberSunasara Imdadhusen9-Sep-11 3:19 
GeneralMy vote of 5 PinmemberNanda_MR13-Mar-11 23:34 
GeneralI laughed a lot :) PinmemberMember 102216226-Oct-10 23:17 
GeneralYou missed the point. PinmemberElagizy27-Oct-10 4:14 
GeneralMy vote of 5 PinmemberL3CodeProject22-Oct-10 10:47 
GeneralSimply Great!! Pinmemberrahu_12221-Sep-10 20:37 
Generalthanks :) :) PinmemberArunJoseph 471639512-Sep-10 5:30 
GeneralThank You Pinmemberjb.jigar30-Jul-10 8:39 
GeneralMy vote of 5 Pinmemberjb.jigar30-Jul-10 8:11 
QuestionWhat if the method is a handler of a PropertyChanged Event from an updated class variable? Pinmemberbretddog1-Apr-10 0:01 
AnswerRe: What if the method is a handler of a PropertyChanged Event from an updated class variable? PinmemberElagizy1-Apr-10 7:53 
GeneralIf it doesn't work for anyone try this... PinmemberMichaelsg22-Mar-10 23:14 
GeneralRe: If it doesn't work for anyone try this... PinmemberElagizy1-Apr-10 7:56 
GeneralWrap 'em up :) PinmemberDerek R. White13-Mar-10 15:27 
GeneralThat was absolutely great Pinmemberchowdarysway15-Oct-09 22:34 
GeneralAccess the controls in Thread PinmemberMuhammad Ghufran13-Aug-09 3:21 
JokeSimple and Clear !!! Pinmembermarcos_zanella15-Jul-09 18:29 
GeneralThanks...This is very useful Pinmemberpmlchan18-Jun-09 22:58 
GeneralMy vote of 2 PinmemberDanilo Corallo8-May-09 2:11 
Generalthanks a lot for the tip Pinmemberwyn4429-Oct-08 6:54 
General[Message Removed] PinmemberMojtaba Vali10-May-08 22:22 
GeneralVery Good! Pinmembertedpap8-Apr-08 20:18 
GeneralA lot of thanx man PinmemberMidhunlal G29-Nov-07 21:58 
GeneralI love you man!!!! PinmemberRanceDowner12349-Nov-07 12:07 
GeneralExcellent! PinmemberBogdan Rechi10-Oct-07 10:09 
GeneralGreat thanks !!! Pinmemberiskenderun9-Jul-07 21:30 
Questionwriting all the code in access control? [modified] PinmemberHari Om Prakash Sharma24-Jun-07 19:14 
AnswerRe: writing all the code in access control? Pinmemberelagizy24-Jun-07 20:57 
GeneralThanks again PinmemberElrond15-Jun-07 0:20 
GeneralOutstanding! PinmemberFabio Videira8-Jun-07 10:10 
GeneralIt simply works :-) PinmemberDiego F.24-May-07 4:58 
Generalcross thread operation Pinmemberevergreen50115-May-07 4:07 
Generalthis is easy yet brilliant Pinmembersunlight2-May-07 17:46 
QuestionPlease excuse my ignorance... PinmemberHD-P26-Apr-07 10:38 
GeneralAny Solution for Same In .net 3.0 Pinmembersumit_4palz24-Apr-07 23:04 
GeneralRe: Any Solution for Same In .net 3.0 Pinmemberelagizy25-Apr-07 3:00 
AnswerRe: Any Solution for Same In .net 3.0 PinmemberMaxtorM1236-Nov-08 5:50 
QuestionHOW TO SOLVE: "Cross thread operation not valid" (DIRTY WAY) PinmemberMiroslav Stampar12-Apr-07 4:16 
GeneralClear, concise and works perfectly! PinmemberVorTechS11-Apr-07 21:21 

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