具有返回值的多线程:vb.net [英] Multi Threading with Return value : vb.net
问题描述
我创建了一个threads
列表来实现Multi Threading
,它们都接受一个返回值的单个函数.以下是我尝试过的代码:
I had created a list of threads
to achieve Multi Threading
that all accepting a single function that returning value. Following is the code that i had tried:
创建线程
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
所以问题是我无法从被调用的函数访问返回值.谁能建议我一些实现这一目标的方法?
So the problem is that i can't access the returning value from the called function. can anyone suggest me some method to achieve this?
预先感谢
推荐答案
在我看来,最简单的方法是使用名为BackgroundWorker
的辅助对象.您可以找到MSDN文档这里.
In my opinion, the easiest way to do this is with a helper object called a BackgroundWorker
. You can find the MSDN documentation here.
以下是使用方法的示例:
Here's an example of how to use it:
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
每个辅助对象都管理自己的线程.调用工作程序的RunWorkerAsync
方法将创建一个新线程,然后引发一个DoWork
事件.处理DoWork
事件的函数在工作程序创建的线程中执行.该功能完成后,工作程序将引发一个RunWorkerCompleted
事件.处理RunWorkerCompleted
事件的函数在原始线程(称为RunWorkerAsync
的线程)中执行.
Each worker object manages its own thread. Calling a worker's RunWorkerAsync
method creates a new thread and then raises a DoWork
event. The function that handles the DoWork
event is executed in the thread created by the worker. When that function completes, the worker raises a RunWorkerCompleted
event. The function that handles the RunWorkerCompleted
event is executed in the original thread, the one that called RunWorkerAsync
.
要在线程之间传递数据,请在事件处理程序中使用EventArgs参数(在上例中为e
).您可以将单个参数传递给RunWorkerAsync
,该参数可通过e.Argument
提供给新线程.要从工作线程向主线程发送另一个方向的数据,请在工作线程中设置e.Result
的值,然后将其值传递给处理线程完成的函数,也称为e.Result
.
To pass data between threads, you use the EventArgs parameters (e
in the example above) in your event handlers. You can pass a single argument to RunWorkerAsync
, which is made available to the new thread via e.Argument
. To send data the other direction, from the worker thread to the main thread, you set the value of e.Result
in the worker thread, which is then passed to function which handles thread completion, also as e.Result
.
您还可以使用ReportProgress
和ProgressChanged
在工作线程仍在执行时在线程之间发送数据(如果您有兴趣,请参阅MSDN文档).
You can also send data between threads while the worker thread is still executing, using ReportProgress
and ProgressChanged
(see MSDN documentation if you're interested).
这篇关于具有返回值的多线程:vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!