472,954 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

Make multithreading do only X threads at a time.

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=-

Nov 20 '05 #1
4 1671
On Thu, 27 May 2004 21:40:15 -0400, Manuel wrote:
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=-


' Dummy code, in a fictious Console application
Option Strict On
Option Explicit On

Imports System
Imports System.Threading

Module modMain
Private Const MAX_THREADS As Integer = 9
Private threads(MAX_THREADS) As Thread

' just so we can get the system thread handle :)
Private Declare Function GetCurrentThreadId Lib "kernel32" () As IntPtr

Public Sub Main()
Dim rnd As New Random

For i As Integer = 0 To MAX_THREADS
threads(i) = New Thread(AddressOf Run)

' randomize the wait...
Thread.Sleep(rnd.Next(100, 1000))

threads(i).Start()
Next

Console.ReadLine()

For i As Integer = 0 To MAX_THREADS
threads(i).Abort()
Next

Console.ReadLine()
End Sub

Private Sub Run()
Dim i As Integer = 0
Dim id As Integer = GetCurrentThreadId().ToInt32()

Try
Do
i += 1
Console.WriteLine("ThreadId {0}: i={1}", _
id, i)
Thread.Sleep(1000)
Loop
Catch ex As ThreadAbortException
Console.WriteLine("ThreadID {0} Aborted!", id)
End Try
End Sub
End Module

I'm not sure if this is what you wanted... But maybe it will help you :)
--
Tom Shelton [MVP]
Nov 20 '05 #2
Tom Shelton wrote:

I'm not sure if this is what you wanted... But maybe it will help you :)


Not quite. What you are doing is starting the threads every X amount of
time, where X is a random number.

I want to be able to run 10 threads and then wait until less than 10 are
finished and load 10 more, rinse and repeat...

Thanks

.................................................. ...............
Posted via TITANnews - Uncensored Newsgroups Access
at http://www.TitanNews.com <<<<

-=Every Newsgroup - Anonymous, UNCENSORED, BROADBAND Downloads=-

Nov 20 '05 #3
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
"Manuel" <Ma*@may.co> wrote in message
news:40***********************@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=-

Nov 20 '05 #4
Manuel,
In addition to my other comments.

The reason I suggested a thread pool (either the built in one, or roll your
own).

Is that starting & stopping 1000 threads, 10 at a time is expensive. It's
generally better to start 10 threads and let each thread process 100
requests. A Thread pool normally has a single queue of requests when a
thread is done working on a request it simply gets the next request in order
& processes it...

Using the ISynchronizeInvoke interface you could setup the ThreadPool so
that it was able to raise an event on your main Thread (your UI thread) to
notify it when all the requests were done... (The thread pool object would
have an ISynchronizeInvoke variable, when the last request finished it would
use ISynchronizeInvoke.Invoke to invoke a delegate, that raised an event.
The ISynchronizeInvoke variable would hold an instance of your form, causing
ISynchronizeInvoke.Invoke to run on the UI thread. Using ISynchronizeInvoke
to raise the event would mean the main form would not need to poll to see if
all the requests were done or not...

Hope this helps
Jay

"Manuel" <Ma*@may.co> wrote in message
news:40***********************@news.titannews.com. ..
Tom Shelton wrote:

I'm not sure if this is what you wanted... But maybe it will help you :)


Not quite. What you are doing is starting the threads every X amount of
time, where X is a random number.

I want to be able to run 10 threads and then wait until less than 10 are
finished and load 10 more, rinse and repeat...

Thanks

.................................................. ..............
Posted via TITANnews - Uncensored Newsgroups Access
>>>> at http://www.TitanNews.com <<<<

-=Every Newsgroup - Anonymous, UNCENSORED, BROADBAND Downloads=-

Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: gianguz | last post by:
The question is about the possible use of inlining to improve performance in a heavy multithreading environment (200+ threads). If we have to work with applications in which threads aren't I/O...
47
by: mihai | last post by:
What does the standard say about those two? Is any assurance that the use of STL is thread safe? Have a nice day, Mihai.
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
5
by: Yuliaan | last post by:
Hi everyone! In program I wrote, I try to run 300 threads, when only 10 is running in the same time. I use array to create 300 threads, but first I start only 10, after that I add one thread when...
5
by: sarge | last post by:
I would like to know how to perform simple multithreading. I had created a simple form to test out if I was multithreading properly, but got buggy results. Sometime the whole thig would lock up...
16
by: who be dat? | last post by:
Consider the following code which enables multithreading in an ASP.Net application I'm writing: Code in global.asx Application_start subroutine, Threadnotify is declared Globally as a new thread...
2
by: Pradnya Patil | last post by:
hi , I am trying to draw ' html div-tag ' on the screen which will resemble a rectangle through vb.net code. I want it to be drawn faster...so I introduced multithreading using Threadpool. I...
0
luke14free
by: luke14free | last post by:
Hello, I was trying to run a multithreading example with python on my phone that lets me to record and play at the same time.I've found an example somewhere but, as expected it doesnt work,,,could...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.