Solved

Cross-thread operation not valid: Control

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 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