views:

89

answers:

3

Currently I create a thread the following way (the normal way)

Public loginThread As Thread
Public loginThreadStart As New ThreadStart(AddressOf LogIntoWhatever)
Public callLoggedIn As New MethodInvoker(AddressOf loggedIn)

However, what I want to be able to do is something along the lines of (this obviously does not work, and is entirely pseudocode)

dim i as integer = 0
for i = 0 to i = 25
Public loginThread(i) as Thread
Public loginThreadStart(i) as New ThreadStart(AddressOf LogIntoWhatever)
next i
Public callLoggedIn as new MethodInvoker(AddressOf loggedIn)

Where I could change 25 to whatever I wanted and have that number of threads created. They would all be running an identical sub which does do not make calls of any kind to each other, they don't need knowledge of each other. Is anything like this possible? If so, direction towards a solution would help.

Thanks in advance.

A: 

Here is an example using a simple thread pool.

rdkleine
+2  A: 

Try the following

Public Function RunThreads(count as Integer, start As ThreadStart) As List(Of Thread)
  Dim list as New List(Of Thread)
  For i = 0 to count -1 
    Dim thread = new Thread(start)
    thread.Start()
    list.Add(thread)
  Next
  Return list
End Function

Or using the thread pool

Public Sub RunThreads(count as Integer, callBack as WaitCallBack) 
  For i = 0 To count-1
    ThreadPool.QueueUserWorkItem(callBack)
  Next
End Sub
JaredPar
This unfortunately has an error on the "Return thread". 'Thread' is a type and cannot be used as an expression.
bahhumbug
changed the return to list and this works as expected now. Thanks!
bahhumbug
A: 

Have a look at this Smart Thread Pool, though it is written in C#.

alexs