当选项严格为OFF时,工作正常。 ON,我得到重载解析失败:
Dim _thread1 As Thread
Private Sub test2(boolTest As Boolean)
' Do something
End Sub
'
Private Sub test()
_thread1 = New Thread(AddressOf test2)
_thread1.Start(True)
End Sub
重载解析失败,因为无法使用以下参数调用可访问的“新建”:
'Public Sub New(start As System.Threading.ParameterizedThreadStart)':Option Strict On不允许缩小方法'Private Sub test2(boolTest As Boolean)'和委托'Delegate Sub ParameterizedThreadingStart(obj As)之间的隐式类型转换对象)”
'Public Sub New(start As System.Threading.ThreadStart)':方法'Private Sub test2(boolTest As boolean)'没有与委托'Delegate Sub ThreadStart()'兼容的签名。
我是线程的新手..没有参数的函数似乎很好,但WITH参数?强硬。我怎样才能做到这一点?我已经搜索过,大多数时候只看java / js回答这个问题。
答案 0 :(得分:5)
以这种方式启动线程时,您的函数必须具有一个或多个参数。如果指定一个参数,则必须来自类型Object
。
在您的函数中,您只需将此object参数转换为您的数据类型:
private sub startMe(byval param as Object)
dim b as Boolean = CType(param, Boolean)
...
end sub
如果要传递多个参数,可以将它们放在一起,如下所示:
public class Parameters
dim paramSTR as String
dim paramINT as Integer
end class
private sub startMe(byval param as Object)
dim p as Parameters = CType(param, Parameters)
p.paramSTR = "foo"
p.paramINT = 0
...
end sub
开始你的主题:
dim t as new Thread(AddressOf startMe)
dim p as new Parameters
p.paramSTR = "bar"
p.oaramINT = 1337
t.start(p)
答案 1 :(得分:1)
它看起来像是因为你所委托的方法有一个布尔参数:' ...不允许缩小...'更改签名以使用Object。
答案 2 :(得分:1)
您应该关注al-eax's answer,但另一种方法是根本不传递Thread.Start()
函数中的参数,而是在test
子...中评估它
Dim _thread1 As Thread
Private Sub test()
If someTest = True then
_thread1 = New Thread(AddressOf test2)
_thread1.Start()
End If
End Sub
Private Sub test2()
/.../
End Sub
...或将其声明为全局变量...
Dim _thread1 As Thread
Dim boolTest As Boolean
Private Sub test()
boolTest = True
_thread1 = New Thread(AddressOf test2)
_thread1.Start()
End Sub
Private Sub test2()
If boolTest = True Then
/.../
End If
End Sub