-1

I need to be able to pass the thread name into a subroutine in order to abort it when I need to.

So I have this so far:

 Dim BrowserSpawn As New System.Threading.Thread(AddressOf BrowserSub)
    BrowserSpawn.Start()

 Private Async Sub BrowserSub(BrowserSpawn)
...
End Sub

Because the subroutine creates a browser within Form1 groups I needed to invoke access to these controls within the sub.

Note: This works fine when I'm not passing in the thread name.

 If Me.GroupNamehere.InvokeRequired Then
            Me.GroupNamehere.Invoke(New MethodInvoker(AddressOf BrowserSub))
        Else
           'Do nothing
        End If

When I'm passing in the thread name these become a problem when trying to compile:

Method does not have a signature compatible with delegate 'Delegate Sub MethodInvoker()'.

I'm hoping this is just a syntax thing but I can't seem to get it to work. Is there any way I'm able to pass in this thread name without breaking my invokerequired check?

If I try and change it to the obvious:

Me.GroupNamehere.Invoke(New MethodInvoker(AddressOf BrowserSub(BrowserSpawn)))

It tells me Addressof operand must be the name of a method (without parentheses). Although without the parentheses it's not happy either so I don't know where to go from here.

/edit:

Stumbled across How can I create a new thread AddressOf a function with parameters in VB?

Which seems to confirm what I was trying passing something like:

Private Sub test2(Threadname As Thread)
    ' Do something
End Sub

And the sub seems happy with that. But I'm not sure how to do that without breaking the invoker part.

 Me.GroupNameHere.Invoke(New MethodInvoker(AddressOf SubNameHere))

Works normally. If SubNameHere() becomes SubNameHere(threadname as thread) then that seems happy enough but the invoke line breaks and doesn't want more than the address of.

Community
  • 1
  • 1
Max Better
  • 61
  • 10
  • When defining a Sub the part in the `()` is for defining the parameters not passing an object. `Public Sub Foo(fubar As String)` – OneFineDay Dec 19 '15 at 04:35
  • Unless I'm missing something MSDN says you can pass objects as well? https://msdn.microsoft.com/en-us/library/bxa9y69d(v=vs.90).aspx – Max Better Dec 19 '15 at 04:51
  • Not in the declaration! You pass objects to it when you call it. `Foo("something")` – OneFineDay Dec 19 '15 at 06:00
  • I've tried calling the thread and passing in the thread name like BrowserSpawn.Start(BrowserSpawn) but if the sub is just BrowserSub() then it's not using it. If it's BrowserSub(BrowserSpawn) it seems to be fine using the thread but it breaks the invoker code. – Max Better Dec 19 '15 at 06:44

1 Answers1

-1

Two slight syntax changes sorted it:

Private Async Sub SubName(ThreadNameAs Thread)

and

GroupName.Invoke(New MethodInvoker(Sub() Me.SubName(ThreadName)))
Max Better
  • 61
  • 10