具有返回值的多线程:vb.net

时间:2014-09-18 11:46:38

标签: vb.net multithreading

我创建了一个threads列表,以实现Multi Threading所有接受返回值的单个函数。以下是我尝试过的代码:

创建主题

  Dim txt as String
  For i As Integer = 0 To 15
      txt = ""
      txt = UCase(array.Item(i))
      Dim tempThread As Threading.Thread = New Threading.Thread(AddressOf threadRead)
      tempThread .Start(txt) ' start thread by passing value to it
      Threads.Add(tempThread ) 'Add thread to the list
      'Here i want to add the return value from the thread to RichTextbox
      'as follows
      RichTextBox1.Text = RichTextBox1.Text & vbNewLine & tempThread.ReturnValue
  Next

线程解决的功能

  Public Function threadRead(ByVal txtInput As String) As String
       Dim stroutput As String = ""
       ' Some operations are performed here
       Return stroutput
  End Function

所以问题是我无法从被调用函数访问返回值。任何人都可以建议我实现这个目标吗?

提前致谢

2 个答案:

答案 0 :(得分:4)

在我看来,最简单的方法是使用一个名为BackgroundWorker的辅助对象。您可以找到MSDN文档here

以下是如何使用它的示例:

Private Sub RunFifteenThreads()
   For i As Integer = 1 To 15
      Dim some_data As Integer = i
      Dim worker As New System.ComponentModel.BackgroundWorker
      AddHandler worker.DoWork, AddressOf RunOneThread
      AddHandler worker.RunWorkerCompleted, AddressOf HandleThreadCompletion
      worker.RunWorkerAsync(some_data)
   Next
End Sub

Private Sub RunOneThread(ByVal sender As System.Object, _
                         ByVal e As System.ComponentModel.DoWorkEventArgs)
   Dim stroutput As String = e.Argument.ToString
   ' Do whatever else the thread needs to do here...
   e.Result = stroutput
End Sub

Private Sub HandleThreadCompletion(ByVal sender As Object, _
                                   ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
   Dim return_value As String = e.Result.ToString
   RichTextBox1.Text = RichTextBox1.Text & vbNewLine & return_value
End Sub

每个工作对象管理自己的线程。调用worker的RunWorkerAsync方法会创建一个新线程,然后引发DoWork事件。处理DoWork事件的函数在worker创建的线程中执行。该函数完成后,工作人员会引发RunWorkerCompleted事件。处理RunWorkerCompleted事件的函数在原始线程中执行,即调用RunWorkerAsync的线程。

要在线程之间传递数据,请在事件处理程序中使用EventArgs参数(上例中的e)。您可以将单个参数传递给RunWorkerAsync,该e.Argument可通过e.Result提供给新线程。要从另一个方向发送数据,从工作线程到主线程,您在工作线程中设置e.Result的值,然后将其传递给处理线程完成的函数,也作为ReportProgress

您还可以在工作线程仍在执行时使用ProgressChanged和{{1}}在线程之间发送数据(如果您有兴趣,请参阅MSDN文档)。

答案 1 :(得分:0)

这是一个想法

    Event ValueAcquired(sender As Object, e As ValueAcquiredEventArgs)
    Delegate Sub delValueAcquired(sender As Object, e As ValueAcquiredEventArgs)
    Sub AllocateThread()
        Dim thStart As New System.Threading.ParameterizedThreadStart(AddressOf threadEntry)
        Dim th As New Threading.Thread(thStart)
        th.Start()
    End Sub
    Sub threadEntry()
        threadRead("whatever this is")
    End Sub
    Sub notifyValueAcquired(sender As Object, e As ValueAcquiredEventArgs)
        If Me.InvokeRequired Then
            Me.Invoke(New delValueAcquired(AddressOf notifyValueAcquired), sender, e)
        Else
            RaiseEvent ValueAcquired(sender, e)
        End If
    End Sub
    Public Sub threadRead(ByVal txtInput As String)
        Dim stroutput As String = ""
        ' Some operations are performed here
        'raise your event
        notifyValueAcquired(Me, New ValueAcquiredEventArgs(stroutput))
    End Sub
    Sub me_valueAcquired(sender As Object, e As ValueAcquiredEventArgs) Handles Me.ValueAcquired
        'Use e.Value to access the retrun value of threadRead
    End Sub
    Public Class ValueAcquiredEventArgs
        Public Value As String
        Sub New(value As String)
            Me.Value = value
        End Sub
    End Class