如何使用 ThreadPool 而不每次都创建 Sub foo(o as object) ?
每次我想在 ThreadPool 中创建一个线程时,我都会创建一个愚蠢的小函数,如下面的 Worker_O 。
Sub Worker(ByVal i As Integer)
'do important stuff
End Sub
Sub Worker_O(ByVal o as Object)
Worker(CType(o, Integer))
End Sub
Sub MakeThread()
Dim worker1 as new Threading.WaitCallback(AddressOf Worker_O))
Threading.ThreadPool.QueueUserWorkItem(worker1)
End Sub
VB .net中有没有一种方法可以在不创建Worker_O的情况下从Sub(i作为整数)转换为Sub(o作为对象)? Worker_O 对我来说很难看。
编辑:我正在使用 Option Explicit On,所有警告都是错误,就像一个好的程序员应该做的那样。
Every time that I want to make a thread in the ThreadPool I make a stupid little function like Worker_O below.
Sub Worker(ByVal i As Integer)
'do important stuff
End Sub
Sub Worker_O(ByVal o as Object)
Worker(CType(o, Integer))
End Sub
Sub MakeThread()
Dim worker1 as new Threading.WaitCallback(AddressOf Worker_O))
Threading.ThreadPool.QueueUserWorkItem(worker1)
End Sub
Is there a way in VB .net to cast from Sub(i as integer) to Sub(o as object) without making Worker_O? Worker_O is ugly to me.
Edit: I'm using Option Explicit On and all warnings are errors, like a good programmer should.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
lambda 可以干净地解决这个问题:
但是,可以调用 Sub 的 lambda 直到 VS2010 才可用。
A lambda can cleanly solve this:
However, lambdas that can call a Sub won't be available until VS2010.