6

When option strict is OFF, works fine. ON, I get overload resolution failure:

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()'.

https://i.stack.imgur.com/pcilv.png

I'm new to threading .. a function without parameters seems just fine, but WITH parameters? Tough. How can I do this? I searched already and mostly see java/js only answering this question.

Justin Ryan
  • 1,409
  • 1
  • 12
  • 25
  • possible duplicate of [How to pass multiple parameters in thread in VB](http://stackoverflow.com/questions/4018282/how-to-pass-multiple-parameters-in-thread-in-vb) – Zohar Peled May 11 '15 at 08:06
  • Also, [read this.](https://msdn.microsoft.com/en-us/library/ts553s52(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1) – Zohar Peled May 11 '15 at 08:07
  • 1
    In the future, please try to include the relevant code, as well as any error messages, as part of the question. – Justin Ryan May 11 '15 at 08:13

3 Answers3

6

When you start a thread this way your function must have one or less parameters. If you specify one parameter, it must be from type Object.

In your function you can simply cast this object parameter to your datatype:

private sub startMe(byval param as Object)
     dim b as Boolean = CType(param, Boolean)
     ...
end sub

When you want to pass multiple parameters, you can put them together into a class like this:

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

To start your Thread:

dim t as new Thread(AddressOf startMe)
dim p as new Parameters
p.paramSTR = "bar"
p.oaramINT = 1337
t.start(p)
al-eax
  • 712
  • 5
  • 13
1

It looks like it's because the method you're delegating to has a Boolean parameter: '...does not allow narrowing...' Change the signature to use Object.

David Osborne
  • 6,436
  • 1
  • 21
  • 35
1

You should follow al-eax's answer, but another way would be to not pass parameters in the Thread.Start() function at all, but rather evaluate it in the test sub...

Dim _thread1 As Thread

Private Sub test()
    If someTest = True then    
        _thread1 = New Thread(AddressOf test2)
        _thread1.Start()
    End If
End Sub

Private Sub test2()
    /.../
End Sub

...or declare it as a global variable...

Dim _thread1 As Thread
Dim boolTest As Boolean

Private Sub test()
    boolTest = True

    _thread1 = New Thread(AddressOf test2)
    _thread1.Start()
End Sub

Private Sub test2()
    If boolTest = True Then
        /.../
    End If
End Sub
Community
  • 1
  • 1
Viktor Ek
  • 353
  • 1
  • 13
  • You could use this when you create only one thread. I am not sure if nativ datatypes like bool & int are thread save in .net. You should not use a global variable this way to parameterize multiple threads. – al-eax May 11 '15 at 08:42
  • 1
    Booleans are thread safe, according to MSDN. You never mentioned you wanted to use multiple new threads nor that they all depend on the same boolean parameter, but if that is the case you should follow al-eax's answer. – Viktor Ek May 11 '15 at 09:07