我正在尝试学习线程,我遇到了一些我无法解决的问题:
想象一下,我的代码中有这个:
Public Sub Compute(ByVal argument1 As String, ByVal argument2 As String)
' Code to be run that I'd like on a background worker thread
End Sub
我想把它变成这样的东西:
Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click
BackGroundWorker1.RunWorkerAsync(argument1, argument2)
End Sub
Private Sub BackGroundWorker1_DoWork(sender As Object, e As ComponentModel.DoWorkEventArgs) Handles BackGroundWorker1.DoWork
'Code to be run that I'd like on a background worker thread.
'This section should be able to use argument1 and argument2.
End Sub
我想知道这是否应该是我应该做的事情,如果不是我在哪里/哪里可以改进代码来实现我想要的结果,但我觉得我有点超出我的深度有这个。
答案 0 :(得分:1)
使用类对象:
Public Class Foo
Public foobar As String
Public fubar As String
End Class
Dim _foo As New Foo
_foo.foobar = "great foo"
_foo.fubar = "wonderful foo"
worker.RunWorkerAsync(_foo)
然后将其转回DoWork
事件。
Private Sub worker_DoWork(...) Handles ...
Dim foo = DirectCast(e.Argument, Foo)
'now you can use the properties you set on the Foo object.
End Sub