gpt4 book ai didi

vb.net - 如何在VB中创建一个带有参数的函数的新线程AddressOf?

转载 作者:行者123 更新时间:2023-12-04 23:45:15 25 4
gpt4 key购买 nike

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()'.



http://i.imgur.com/X0mH9tm.png

我是线程的新手……一个没有参数的函数似乎很好,但是有参数吗?艰难的。我怎样才能做到这一点?我已经搜索过,并且大多只看到 java/js 只回答了这个问题。

最佳答案

当你以这种方式启动一个线程时,你的函数必须有一个或更少的参数。如果指定一个参数,它必须来自类型 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)

关于vb.net - 如何在VB中创建一个带有参数的函数的新线程AddressOf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30162382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com