加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 07:16:28 所属栏目:百科 来源:网络整理
导读:当选项严格为OFF时,工作正常. ON,我得到重载解析失败: Dim _thread1 As ThreadPrivate Sub test2(boolTest As Boolean) ' Do somethingEnd Sub'Private Sub test() _thread1 = New Thread(AddressOf test2) _thread1.Start(True)End Sub Overload resolutio
当选项严格为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

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

我是新线程..没有参数的函数似乎很好,但WITH参数?强硬.我怎样才能做到这一点?我已经搜索过了,大多数时候只看java / js回答这个问题.

解决方法

以这种方式启动线程时,您的函数必须具有一个或多个参数.如果指定一个参数,则它必须来自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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读