How to solve "Cross thread operation not valid"

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:

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).VisibleMe.OpacityMe.ShowInTaskbar