引用线程中的对象(类)失败

时间:2012-06-08 18:10:53

标签: vb.net multithreading class

在线程中引用对象(类)失败

我有一个对象(“一个vb类”)需要在一个单独的线程上运行并与U / I通信。

即使使用示例代码,我也无法让对象在线程中运行。我用两个例子来说明这个问题。第一个调用表单中的函数,它完美地工作。第二个创建一个对象并执行相同的调用,但失败。


示例#1 [工作](在表单中调用)

 Private Delegate Sub FuctionToBeRunInThreadDelegate

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    Console.WriteLine("Thread sample within FORM")

    Dim Thread As New System.Threading.Thread(AddressOf FuctionToBeRunInThread)
    Thread.Start()

    Console.WriteLine("Thread sample within FORM completed")

 End Sub

 Public Sub FuctionToBeRunInThread()

    If Me.InvokeRequired Then
        Console.WriteLine("Inside new thread....")
        Me.Invoke(New FuctionToBeRunInThreadDelegate(AddressOf FuctionToBeRunInThread))
    Else
        Console.WriteLine("oFuctionToBeRunInThread")
    End If

  End Sub

在表单中调用函数时输出到控制台是:
FORM中的线程样本 FORM中的线程样本已完成
内部新线程....
oFuctionToBeRunInThread

这就是我的预期。该线程已异步启动。结束非常快,函数知道它在一个线程内。




示例#2 [非工作](调用相同的功能,但这次是在对象内)
在我的第二个例子中,我使用包含类的外部文件并以相同的方式创建线程。

这是主要形式:

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

    Console.WriteLine("Thread sample using object started.")

    Dim oEngine As New objTest()
    Dim oThread As New System.Threading.Thread(AddressOf oEngine.oFuctionToBeRunInThread)
    oThread.Start()

    Console.WriteLine("Thread sample using object completed.")


End Sub


这是包含类定义的外部文件:

   Public Class objTest

   Private Delegate Sub oFuctionToBeRunInThreadDelegate()

   Public Sub oFuctionToBeRunInThread()

        If frmInitial.InvokeRequired Then
            Console.WriteLine("Inside new thread....")
            frmInitial.Invoke(New oFuctionToBeRunInThreadDelegate(AddressOf oFuctionToBeRunInThread))
        Else
            Console.WriteLine("oFuctionToBeRunInThread")
        End If

    End Sub
   End Class

在这种情况下,输出显示该函数未在单独的线程中运行。 如下:

使用对象的线程样本开始 使用对象完成线程样本。
oFuctionToBeRunInThread



摘要:当线程创建对象时,看起来该对象实际上并未在线程内运行,否则form.InvokeRequired将为true。我有其他更复杂的例子,这是经过充分验证和证明的,但我提供了这两个作为我问题的一个非常简单的演示。

如何让一个线程“拥有”一系列单独的对象这样对象就可以异步运行,并且可以独立地报告给U / I?

1 个答案:

答案 0 :(得分:1)

调试代码后,似乎frmInitial.InvokeRequired无法正确执行。

=============================================== ======

所以我又回来了(当然有一个解决方案):

Public Class frmInitial

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Console.WriteLine("event started")

        Dim obj As New ObjTest()
        Dim threadobj As New System.Threading.Thread(AddressOf obj.dosomework)
        threadobj.Name = "debugme"

        'passing the current form reference to the thread function
        threadobj.Start(Me)

        Console.WriteLine("event completed")
    End Sub
End Class

screen snapshot

Public Class ObjTest
        Private Delegate Sub dosomeworkDelegate(ByVal f As Form)

        Public Sub dosomework(ByVal f As Form)
            ' Check if the form can be accessed from the current thread.
            If Not f.InvokeRequired Then
                ' Access the form directly.
                Console.WriteLine("delegate executed")
                f.Controls("textbox1").Text = "thread invoked successfuly"
            Else
                ' Marshal to the thread that owns the form. 
                Dim del As dosomeworkDelegate = AddressOf dosomework
                Dim param As Object() = {f}
                Console.WriteLine("executing delegate")
                f.BeginInvoke(del, param)
            End If
        End Sub

End Class
相信我比研究我的数值分析考试更好: - )