Solved

Cross-thread operation not valid: Control

Posted on 2010-11-26
2
534 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

632 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