从线程实例化的对象调用委托。

时间:2022-12-17 21:02:14

Does anyone know how to invoke a function on the main Form page from an instantiated class that runs in a thread?

是否有人知道如何从一个线程中运行的实例化的类调用主表单页面上的函数?

Public Class Test
    Public Sub Temp()
        Try
            'Operations here
        Catch ex as Exception
            Form1.Invoke(New Form1.LogMessageDelegate(AddressOf frmIP.LogMessage), "Error Message To Be Outputted to TextBox on Main Form")
        End Try
    End Sub
End Class

I would have it return a string in a function, but I only invoke the textbox message if there is an exception. Thank you for your time!

我将让它在函数中返回一个字符串,但如果有异常,我只调用文本框消息。感谢您的时间!

1 个解决方案

#1


0  

Public Class Test
 Public Event ErrorMessage(errStr As String)
 Public Sub Temp()
    Try
        'Operations here
    Catch ex as Exception
        RaisEvent ErrorMessage(ex.Message)
    End Try
 End Sub
End Class

Public Class Form1
  Private Sub form1_load() Handles Me.Load
    Dim trd As New Thread(AddressOf RunTestClass)
    trd.Start()
  Ens Sub

  Private Sub RunTestClass()
    Dim _test As New Test
    Addhandler _test.ErrorMessage, AddressOf Test_ErrorMessage
    _test.Temp()
  End Sub

  Private Sub Test_ErrorMessage(errStr As String)
    Me.Invoke(Sub()
              'run code here for UI thread
              End Sub)
  End Sub
End Class

#1


0  

Public Class Test
 Public Event ErrorMessage(errStr As String)
 Public Sub Temp()
    Try
        'Operations here
    Catch ex as Exception
        RaisEvent ErrorMessage(ex.Message)
    End Try
 End Sub
End Class

Public Class Form1
  Private Sub form1_load() Handles Me.Load
    Dim trd As New Thread(AddressOf RunTestClass)
    trd.Start()
  Ens Sub

  Private Sub RunTestClass()
    Dim _test As New Test
    Addhandler _test.ErrorMessage, AddressOf Test_ErrorMessage
    _test.Temp()
  End Sub

  Private Sub Test_ErrorMessage(errStr As String)
    Me.Invoke(Sub()
              'run code here for UI thread
              End Sub)
  End Sub
End Class