genespos genespos - 8 days ago 6
Vb.net Question

Add Thread to a List with Option Strict

I wrote this code:

Dim TL As New List(Of Thread)
For x = 0 To 10
TL.Add(New Thread(AddressOf ThreadWork))
TL(x).Start(x)
Next


It works fine with
Option Strict Off
.

But if I turn
Option Strict On
I get error (overload resolution failed) on
TL.Add(New Thread(AddressOf ThreadWork))


I also tried

Dim T As New Thread(AddressOf ThreadWork)
TL.Add(T)


but still get error.

I searched the web and I found the same syntax on msdn (here), so I don't understand where I'm wrong.

ThreadWork is:
Private Sub ThreadWork(ByVal W As Integer)


My goal is to get some threads into a List or into an Array so any hint on this way is apprecciated

Answer

Try this code:

        Dim TL As New List(Of Thread)
        Dim T As New Thread(Sub(w As Object)
                                ThreadWork(CInt(w))
                            End Sub)
        TL.Add(T)

your method

    Private Sub ThreadWork(ByVal W As Integer)

    End Sub
Comments