Solved

Cross-thread operation not valid: Control

Posted on 2010-11-26
2
530 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

While working on Silverlight and WCF application, I faced one issue where fault exception occurred at WCF operation contract is not getting propagated to Silverlight client. So after searching net I came to know that it was behavior by default for s…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

856 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