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)

Formlabel=lbl

.
.
.
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."
  Source="System.Windows.Forms"
  StackTrace:
       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
LVL 2
boricua1213Asked:
Who is Participating?
 
boricua1213Author Commented:
oh by the way I am using .NET 2.0 (VB2005)
0
 
bchoorCommented:
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")

HTH
~BC
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
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.
0
 
boricua1213Author Commented:
Idle_Mind

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

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

thanks again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.