Manuel,
It sounds like you simply want to use the System.Threading.ThreadPool or
roll your own Thread Pool.
System.Threading.ThreadPool allows by default 25 threads per processor. To
use it you would simply use ThreadPool.QueueUserWorkItem 1000 times with the
same function address... See System.Threading.ThreadPool for details...
Alternatively if you want to limit it to 10 threads, you will need to write
your own Thread Pool. I would create a new Thread Pool class that started 10
Threads. My Thread Pool class would have a System.Collections.Queue object
that represented the requests. Each thread would dequeue an item and work on
it. I would have a special request or other mechanism available to tell each
thread that it is time to exit. I would also include a single
AutoResetEvent, that each thread would wait on to see if an item is in the
Queue. Plus there should be a padlock object (I normally use "New Object")
that you can SyncLock on to ensure that reading & writing to the queue is
properly synchronized.
Writing your own thread pool will not be as easy as I am making it sound,
however it is fairly easy! The above should be enough to get you started,
alternatively you could search google for a sample. Tom's code may help you
get started...
Note I've done the above & posted to the newsgroup my alternative above
using a single thread.
Hope this helps
Jay
[quoted text, click to view] "Manuel" <Man@may.co> wrote in message
news:40b69871$0$87536$a8266bb1@news.titannews.com...
> I have a long function that needs to be done 1000 times. I'm
> multithreading it, but I don't want to load them up all at once, instead
> load them 10 at a time.
>
> So far the only way I can get it to work is by creating a dummy form
> with a timer. On the timer function I test if the number of threads are
> less than 10 then run the remaining ones. This is working fine, but I
> would like to know how to do it without the form.
>
> This is the code I'm trying to use to accomplish the feat without the
form:
>
> Module mdlAny()
> Public Sub Main()
> Do
> FunctionThatCalls10Threads()
>
> PauseThisThing() 'Replace this with
> 'either of the bottom functions
> Loop
> End Sub
>
> Private Sub PauseThisThingThatWorks()
> 'It works with this code:
> 'On the Timer event, I call
> 'the FunctionThatChecksTheNumberOfThreads()
> 'to see if I can close the form.
> '(and hence continue with the program)
>
> Dim wf As WaitForm = New WaitForm
> wf.ShowDialog()
> End Sub
>
> Private Sub PauseThisThingThatDOESNOTWork()
> 'It doesn't work with this code for waiting
>
> Dim RetValue as Boolean
> Do
> System.Threading.Thread.CurrentThread.Sleep(1000)
> RetValue = FunctionThatChecksTheNumberOfThreads()
> Loop Until RetValue
> End Sub
> End Module
>
>
>
> ................................................................
> Posted via TITANnews - Uncensored Newsgroups Access
> >>>> at
http://www.TitanNews.com <<<<
> -=Every Newsgroup - Anonymous, UNCENSORED, BROADBAND Downloads=-
>