|
-
May 5th, 2017, 03:51 PM
#1
Thread Starter
Junior Member
[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:
Public Sub MainSub()
Dim t1 As New Threading.Thread(AddressOf SecondSub)
Dim t2 As New Threading.Thread(AddressOf SecondSub)
t1.Start()
t2.Start()
t1.Join()
t2.Join()
End Sub
Public Sub SecondSub()
'Do some multi-threaded operation here
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:
Public Sub MainSub()
Generate_Multithreading("SecondSub")
End Sub
Public Sub Generate_Multithreading(ByVal targetSub as String)
Dim t1 As New Threading.Thread(AddressOf targetSub)
Dim t2 As New Threading.Thread(AddressOf targetSub)
t1.Start()
t2.Start()
t1.Join()
t2.Join()
End Sub
Public Sub SecondSub()
'Do some multi-threaded operation here
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|