1

I Use a thread to send an email. I need write text in a label to let user know when email was send in a form of budgets.

' Option Strict is off

Function on class form.

Public Sub ChangeLblText(ByVal msg As String) ' LblIconos.modifiers = public (In form)
     lblIconos.Text = msg
End Sub

Declarated class of new thread. (Global declared.)

Public TasksPresu As New TasksClassPresu()

An Icon of Form "Presupuestos" trigger the thread

    Dim Thread1 As New System.Threading.Thread(AddressOf TasksPresu.TaskEmail)
    Thread1.Start()

Class Trigged thread

Public Class TasksClassPresu
    Sub TaskEmail()
    'trigger label change?
     Presupuestos.ChangeLblText("Test")
    end sub
End Class

I tried Invoke metods, delegate functions and adress of form without work, may be i left something.

An example delegate that i used:


In Form Presupuestos class:

Public Delegate Sub LabelDelegate(ByVal Msg As String)

In Global Module to declare most of variables

 Public Llamada As Presupuestos.LabelDelegate 

Function on class form.

Public Sub ChangeLblText(ByVal msg As String) ' LblIconos.modifiers = public (In form)
     lblIconos.Text = msg
End Sub

Class Trigger

Public Class TasksClassPresu
    Sub TaskEmail()
        Llamada.Invoke("prueba")
    End Sub
End Class

When run this make an error in this line: lbliconos.text = msg Error: "Illegal operation through threads. It had access to the control "lbliconos" from a thread other than that which was created"

krespy
  • 11
  • 1

1 Answers1

0

I don't have the details in front of me, but you need to check lblIconos.InvokeRequired in ChangeLblText and Invoke a delegate call itself when true.

This question, and many others like it, gives more details.

Community
  • 1
  • 1
Mark Hurd
  • 10,665
  • 10
  • 68
  • 101