• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

Set Control text properties from another class/thread

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
  • 3
  • 2
1 Solution
boricua1213Author Commented:
oh by the way I am using .NET 2.0 (VB2005)
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")

Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

boricua1213Author Commented:

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
boricua1213Author Commented:
thanks Idle_mind,

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

thanks again

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now