Solved

Cross-thread operation not valid: Control

Posted on 2010-11-26
2
524 Views
Last Modified: 2012-05-10

I am trying to load a combo box from a web service.  I am doing this through a callback Display lists.  Display lists receives the Lists parameter fine when called. So the web service call itself is working fine.   But when i tries to bind the list to the combo box i get the error  "Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on."  

I have tried re-factoring this in many ways using background workers etc I even tried to create my own custom control who sole purpose was to load this data.  All methods seem to give me the same error.




 
Private WithEvents M_List As New IContact.List



  

Private Sub ctrl_ImportGroups_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.M_List.LoadLists()

End Sub







Public Sub DisplayLists(ByVal Lists As List(Of IContact.List)) Handles M_List.onLoadListsCompleted

        Me.cmbIContactList.DataSource = Lists

        Me.cmbIContactList.ValueMember = "listid"

        Me.cmbIContactList.DisplayMember = "name"

End Sub

Open in new window

0
Comment
Question by:Kevin Robinson
2 Comments
 
LVL 13

Accepted Solution

by:
Naman Goel earned 500 total points
ID: 34218607
The Reason:
If you are from the old COM/COM+ Days, you must remember that an STA (Single Threaded Apartment) can only be accessed by the thread that originally created it. All the controls on the form are created by the main thread of the application, so only the main thread can update it. When your asynch webservice call returns it is calling the DisplayLists(ByVal Lists As List(Of IContact.List)) Handles M_List.onLoadListsCompleted
event handler in the context of a different thread. This thread is not allowed to touch any controls on the form

Please refer to modified code
Private WithEvents M_List As New IContact.List

  
Private Sub ctrl_ImportGroups_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.M_List.LoadLists()
End Sub

Private Delegate Sub UpdateUI(ByVal Lists As List(Of IContact.List))


Public Sub DisplayLists(ByVal Lists As List(Of IContact.List)) Handles M_List.onLoadListsCompleted
        Me.Invoke(New UpdateUI(AddressOf UpdateUIMethod), Lists)
End Sub

Public Sub UpdateUIMethod(ByVal Lists As List(Of IContact.List))
	Me.cmbIContactList.DataSource = Lists
        Me.cmbIContactList.ValueMember = "listid"
        Me.cmbIContactList.DisplayMember = "name"
End Sub

Open in new window

0
 
LVL 3

Author Closing Comment

by:Kevin Robinson
ID: 34225141
Great.  Almost had that actully
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now