vb.net - How to Declare new task as SUB with parameters

newbeee picture newbeee · Apr 20, 2013 · Viewed 12k times · Source

As you know we have a new syntax in vb.net with possibility to create inline tasks so we could run it asynchronously.

This is the correct code:

        Dim testDeclaring As New Task(Sub()

                                      End Sub)
        testDeclaring.Start()

but now I need to pass a parameter in the subroutine and I can't find correct syntax for that. Is it possible any way?

Answer

dbasnett picture dbasnett · Apr 20, 2013

If you want to pass a parameter you could do this

    Dim someAction As Action(Of Object) = Sub(s As Object)
                                              Debug.WriteLine(DirectCast(s, String))
                                          End Sub

    Dim testDeclaring As New Task(someAction, "tryme")
    testDeclaring.Start()