Set Control text properties from another class/thread

Posted on 2006-04-25
Last Modified: 2006-11-18
I got a form name form1 in this form I have a label named label1

In my class named MyTapi I have eventhandlers that handle certain events in new threads.  

in Form1 I call this:

dim tapi as New MyTapi("12354",label1)

in my class MyTapi:

Private FormLable as label

Sub New(ext as string, lbl as label)


End Sub

When an event get caught I would like to edit the text of the label

when I do label.text="Change text" in my event handler I get this error:

System.InvalidOperationException was unhandled
  Message="Cross-thread operation not valid: Control 'Label1' accessed from a thread other than the thread it was created on."
       at System.Windows.Forms.Control.get_Handle()
       at System.Windows.Forms.Control.set_WindowText(String value)
       at System.Windows.Forms.Control.set_Text(String value)
       at System.Windows.Forms.Label.set_Text(String value)
       at WindowsApplication1.MyTapi.CallStateEvent() in C:\Documents and Settings\rick.maldonado\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\MyTapi.vb:line 146
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

anyhelp will be much appreciated

Thank You
Question by:boricua1213
    LVL 2

    Author Comment

    oh by the way I am using .NET 2.0 (VB2005)
    LVL 10

    Expert Comment

    You can use a delegate

    In MyTapi Class

    Delegate Sub SetFormLabelText(Value As String)

    Dim delSetFormLabelText As SetFormLabelText

    Sub New()

      delSetFormLabelText = AddressOf FormLabelText
    End Sub

    Sub FormLabelText(Value As String)
      Form1.Label1.Text = Value

    End Sub

    In your eventhandler:
    delSetFormLabelText.Invoke("Your Label Value")

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Your class design seems backwards to me...

    Instead of passing a Label into MyTapi (which makes it tightly coupled to the form and less resuseable), make MyTapi raise a Custom Event with the new Label value.   The Form should then subscribe to this event and use its own Delegate to marshal the call from the different thread onto its own thread.
    LVL 2

    Author Comment


    That sounds great but how do you do that?  This is my first program that uses events and threads.  So a quick tutorial will be much appreciated.

    Thank you
    LVL 85

    Accepted Solution

    LVL 2

    Author Comment

    thanks Idle_mind,

    that works, sorry it took so long to respond I got put on another project.

    thanks again

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Suggested Solutions

    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    This video discusses moving either the default database or any database to a new volume.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    759 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

    11 Experts available now in Live!

    Get 1:1 Help Now