无法从在线程中工作的类函数更新控件

时间:2012-02-27 11:41:35

标签: vb.net

从调用类中的函数的线程更新TextBox不起作用。我试图让这个在DLL中工作(但我尝试过没有)。 DLL接受一个Object作为参数,就像这样;

Sub New(ByVal theObject As Object)

这允许我发送一个像这样的对象:

Public Class anObject
    Public Sub doSomething()
        Form1.TextBox1.Text = "test"
    End Sub
End Class

所以我将这样调用DLL:

Dim theObject As New anObject
Dim theDLL As New SO_TBFromDLL.UpdateTB(theObject)

之后,DLL将启动一个Thread,然后调用“doSomething”

Public Class UpdateTB
    Dim theObjectSet As Object

    Sub New(ByVal theObject As Object)
        theObjectSet = theObject

        'Try to update TB from Thread within current class
        Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf bghandler)
        ctThread.Start()
    End Sub

    'Try to update TB from Thread within current class
    Sub bghandler()
        theObjectSet.doSomething()
    End Sub
End Class

但这不起作用。我也尝试使用委托(下面的代码)。

除了这两个之外,我一直在尝试另外两个变体;

从不在Class / DLL中的线程中尝试3。这很好用。

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf receiverFunction)
    ctThread.Start()
End Sub

receiverFunction为:

'Receiver for Thread that's not in DLL (Button 3)
Private Sub receiverFunction()
    generaldelegatesN("tb1", "test")
End Sub

从不在DLL中的Thread尝试4,但使用Class内部的Function。这也不起作用。

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    Dim theObject As New anObjectWithDelegate
    Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf theObject.doSomething)
    ctThread.Start()
End Sub

anObjectWithDelegate为:

'Unfortunately, with a delegate it still doesn't work (Button2 & Button4)
Public Class anObjectWithDelegate
    Public Sub doSomething()
        Form1.generaldelegatesN("tb1", "test")
    End Sub
End Class

为什么尝试3工作,但尝试4不工作?它的doSomething()函数几乎与receiverFunction相同,但添加的Form1.

除外

授权代码:(如果重要)

'The Delegate functions
Delegate Sub generaldelegates(ByVal param1 As String, ByVal param2 As String)
Public generaldelegatesM As generaldelegates
Public Sub generaldelegatesN(ByVal param1 As String, ByVal param2 As String)
    'Updates control on main thread:
    If Me.InvokeRequired Then
        generaldelegatesM = New generaldelegates(AddressOf generaldelegatesN)
        Me.BeginInvoke(generaldelegatesM, New Object() {param1, param2})
    Else
        Select Case param1
            Case "tb1"
                TextBox1.Text = param2
        End Select
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

您正在尝试对单个类中的外部对象进行硬编码,该类不知道在其他地方实例化了哪些对象。您的类对象需要将文本框引用传递给它,如下所示:

Public Class Form1

    Private myObject = New anObject

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        myObject.doSomething(TextBox1, "test")
    End Sub

End Class

Public Class anObject
    Public Sub doSomething(ByRef obj As Object, ByVal message as String)
        Dim objType = obj.GetType()

        If objType Is GetType(TextBox) Then obj.Text = message

    End Sub
End Class