vb.net 如何在VB中创建一个带有参数的函数的新线程AddressOf?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30162382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create a new thread AddressOf a function with parameters in VB?
提问by al-eax
When option strict is OFF, works fine. ON, I get overload resolution failure:
When option strict is OFF, works fine. 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
Overload resolution failed because no accessible 'New' can be called with these arguments:
'Public Sub New(start As System.Threading.ParameterizedThreadStart)': Option Strict On does not allow narrowing in implicit type conversions between method 'Private Sub test2(boolTest As Boolean)' and delegate 'Delegate Sub ParameterizedThreadingStart(obj As Object)'.
'Public Sub New(start As System.Threading.ThreadStart)': Method 'Private Sub test2(boolTest As boolean)' does not have a signature compatible with delegate 'Delegate Sub ThreadStart()'.
重载解析失败,因为无法使用以下参数调用可访问的“New”:
'Public Sub New(start As System.Threading.ParameterizedThreadStart)':Option Strict On 不允许在方法'Private Sub test2(boolTest As Boolean)'和委托'Delegate Sub ParameterizedThreadingStart(obj As Object)'之间的隐式类型转换中缩小.
'Public Sub New(start As System.Threading.ThreadStart)':方法'Private Sub test2(boolTest As boolean)'没有与委托'Delegate Sub ThreadStart()'兼容的签名。
I'm new to threading .. a function without parameters seems just fine, but WITH parameters? Tough. How can I do this? I searched already and mostly see java/js only answering this question.
我是线程的新手……一个没有参数的函数似乎很好,但是有参数吗?艰难的。我怎样才能做到这一点?我已经搜索过,并且大多只看到 java/js 只回答了这个问题。
回答by al-eax
When you start a thread this way your function must have one or less parameters. If you specify one parameter, it must be from type Object.
当你以这种方式启动一个线程时,你的函数必须有一个或更少的参数。如果指定一个参数,它必须来自 type Object。
In your function you can simply cast this object parameter to your datatype:
在您的函数中,您可以简单地将此对象参数转换为您的数据类型:
private sub startMe(byval param as Object)
dim b as Boolean = CType(param, Boolean)
...
end sub
When you want to pass multiple parameters, you can put them together into a class like this:
当你想传递多个参数时,你可以把它们放在一个类中,如下所示:
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
To start your Thread:
要开始您的主题:
dim t as new Thread(AddressOf startMe)
dim p as new Parameters
p.paramSTR = "bar"
p.oaramINT = 1337
t.start(p)
回答by David Osborne
It looks like it's because the method you're delegating to has a Boolean parameter: '...does not allow narrowing...' Change the signature to use Object.
看起来是因为您委托给的方法有一个布尔参数:'...不允许缩小...' 将签名更改为使用 Object.
回答by Viktor Ek
You should follow al-eax's answer, but another way would be to not pass parameters in the Thread.Start()function at all, but rather evaluate it in the testsub...
您应该遵循al-eax 的回答,但另一种方法是根本不在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
...or declare it as a global variable...
...或将其声明为全局变量...
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


