[2005] Multiple Threading Help
I'm making a program that will sign people and preform automated things when logged in to a website. I have a list box of usernames and passwords. I know how to go through them and such but I am trying to make this multi threaded. I need to know if there is some way to create a specific number of threads for the number of items in the list box and if possible, have them numbered 0, 1, 2 etc.
vbnet Code:
For i As Integer = 0 To lbAccs.Items.Count - 1
Me.Main = New System.Threading.Thread(AddressOf TreeSub)
Me.Main.IsBackground = True
Me.Main.Start()
'if they stop the program before all threads have been created
If cmdStart.Text = "Start" Then
SetText(lbLog, Now & " : Program Stopped")
End If
Next
Any help would be great. Thanks.
Re: [2005] Multiple Threading Help
I don't know that there is the precise thing you are asking for, but there are some things that might help. If you just start the thread in a loop like that, then you certainly won't have any way to identify the thread later. However, if you were to create an array of threads, you could give each one a name. Of course, in that case, you might also look at the thread pool, and look at the ManagedThreadID property in particular. That would give you something like a unique id for each thread, but then you would have to decide what to do with it.
Re: [2005] Multiple Threading Help
I think you'll better off using BackgroundWorkers like this:
vb.net Code:
Public Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
For i As Integer = 0 To lbAccs.Items.Count - 1 Step 1
If cmdStart.Text <> "Start" Then
Exit For
Else
Dim bgr As New System.ComponentModel.BackgroundWorker()
AddHandler bgr.DoWork, AddressOf bgr_DoWork
bgr.RunWorkerAsync(i.ToString())
End If
Next
End Sub
Private Sub bgr_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
System.Threading.Thread.CurrentThread.Name = DirectCast(e.Argument, String)
SetText(lbLog, Now & " : Program Stopped")
'etc.
End Sub
Re: [2005] Multiple Threading Help
I'd go with using the ThreadPool, as SH mentioned.
Re: [2005] Multiple Threading Help
Quote:
Originally Posted by Shaggy Hiker
I don't know that there is the precise thing you are asking for, but there are some things that might help. If you just start the thread in a loop like that, then you certainly won't have any way to identify the thread later. However, if you were to create an array of threads, you could give each one a name. Of course, in that case, you might also look at the thread pool, and look at the ManagedThreadID property in particular. That would give you something like a unique id for each thread, but then you would have to decide what to do with it.
I'll look into this approach, although, I also need the array to have specific properties to each array. I need the array to have the proper username and password for each array.
On the other post. I wasn't sure how to properly add BGworkers without adding them directly to the form. I will also try that.
Re: [2005] Multiple Threading Help
I suggested the background workers because dealing with multiple threads has always been hell for me. :D