Results 1 to 8 of 8

Thread: [RESOLVED] Dim t1 As New Threading.Thread(AddressOf VARIABLEHERE)

Threaded View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    25

    [RESOLVED] Dim t1 As New Threading.Thread(AddressOf VARIABLEHERE)

    Hi all,

    Apologies if this has been answered before, I suppose it's possible that I've been Googling incorrectly, though I did my best to look around for this information before posting.

    I am self-taught, so there's a distinct possibility that I am doing this 100% incorrectly. I have a few vb.net programs which invoke threads pointing to different subs, generally the syntax for this would be as follows:

    vb.net Code:
    1. Public Sub MainSub()
    2.     Dim t1 As New Threading.Thread(AddressOf SecondSub)
    3.     Dim t2 As New Threading.Thread(AddressOf SecondSub)
    4.     t1.Start()
    5.     t2.Start()
    6.     t1.Join()
    7.     t2.Join()
    8. End Sub
    9.  
    10. Public Sub SecondSub()
    11.     'Do some multi-threaded operation here
    12. End Sub

    As far as I can tell, this works perfectly. But imagine if I used that method of spawning threads and I wanted 64 threads (rather than 2) running SecondSub at the same time. You'd have to write down Dim t1 through Dim t64, along with 64 Start()'s, and 64 Join()'s. It's a large chunk of code, and I call multithreading a few times throughout my project. I thought I could minimize this by only writing it once in a Generate_Multithreading sub, and then sending the target sub name into it like this:

    vb.net Code:
    1. Public Sub MainSub()
    2.     Generate_Multithreading("SecondSub")
    3. End Sub
    4.  
    5. Public Sub Generate_Multithreading(ByVal targetSub as String)
    6.     Dim t1 As New Threading.Thread(AddressOf targetSub)
    7.     Dim t2 As New Threading.Thread(AddressOf targetSub)
    8.     t1.Start()
    9.     t2.Start()
    10.     t1.Join()
    11.     t2.Join()
    12. End Sub
    13.  
    14. Public Sub SecondSub()
    15.     'Do some multi-threaded operation here
    16. End Sub

    This causes an error: "'AddressOf' operand must be the name of a method (without parentheses)."

    I am confident that this is because my targetSub variable is a String, and AddressOf doesn't accept strings. So what should I make targetSub in order to make AddressOf happy? Is it even possible to point AddressOf at a variable of any kind?

    If not, maybe I'm just going about the multithread generation stupidly? As I said: I am self taught, and I am willing to admit that I know very little.

    I'd appreciate any advice! Thanks in advance!
    Last edited by RichardG; May 8th, 2017 at 01:07 PM. Reason: Added [RESOLVED] to title. :)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width