views:

47

answers:

1

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.

+2  A: 

A lambda can cleanly solve this:

  Sub MakeThread()
    Threading.ThreadPool.QueueUserWorkItem(Function() Worker(42))
  End Sub

  Function Worker(ByVal arg As Integer) As Integer
    ' etc...
  End Function

However, lambdas that can call a Sub won't be available until VS2010.

Hans Passant
So I just change the Sub to Function, return some garbage value in that function, and the lambda will work?
Eyal
Yes (15 chars).
Hans Passant