Multithreading question

F

fniles

I am using VB.NET 2005 and thread.
In each thread I open a database connection and before it exits the thread I
close the database connection.
Is there any way for me to count how many threads are still working so that
if I know there is say 50 threads are working, I want to hold on and do not
create any more thread until the previous threads are finished ?
Thank you
 
E

eBob.com

I'm not one of the heavy hitters here, but what I do in a similar situation
is to use a simple counter. In my case the incrementing doesn't need to be
synchronized. To decrement the counter I use ...

SitesToGo = Interlocked.Decrement(globalinf.TotalSites)

If you need to synchronize the incrementing I am sure there is an
Interlock.Increment method.

When you use counters like this you have to be very sure they get
incremented and decremented in all the right places. It's easy to miss
error handling paths.

Bob
 
J

Just_a_fan

Each thread knows it's number. You could log them in some way. Maybe
have each one set it's element in an array to true coming in and false
going out and you could run the array and count bits before starting up
another one. Fairly cheap on processor time, I expect.

I have been wondering about how to serialize also. No answer seen yet.
Still hoping...

Mike
 
C

cj

I have a class:

Public Class MyThreadCount

Private Shared m_lock As New Object
Private Shared m_threadcount As Int32 = 0

Public Shared Sub Increment()
SyncLock (m_lock)
m_threadcount += 1
End SyncLock
End Sub

Public Shared Sub Decrement()
SyncLock (m_lock)
m_threadcount -= 1
End SyncLock
End Sub

Public Shared ReadOnly Property ThreadCount() As Int32
Get
Dim _count As Int32
SyncLock (m_lock)
_count = m_threadcount
End SyncLock
Return _count
End Get
End Property
End Class

When a thread starts it increments the counter:

MyThreadCount.Increment()

When it ends it decrements the counter:

MyThreadCount.Decrement()

The main program has a timer that looks at MyThreadCount.ThreadCount and
displays how many threads are currently running as an indicator of how
much work the program is doing. Also when the program goes to shutdown
I stop creating new threads and wait for the threadcount to come down to
0 before stopping.

This seems like what Bob suggested but I'm not familiar with Interlock.
Perhaps it does what synclock does and prevents multiple threads from
stepping on each other when incrementing and decrementing the counter.
 
F

fniles

Thank you.
However why are you using a thread to open a connection, as that is the
first step in some real synchronous actions.
In the thread I need to read a few queries from the database, if I open the
connection in the main program and sends the connection to the thread, won't
I run into the error "you can only have 1 datareader per connection", or
something like that ?
 
F

fniles

Thank you.

Can I do the following:
1. check the number of threads
2. if the number of threads >= 100 then loop until the number of threads <
100
like so:

Dim EachMessageThread As Threading.Thread = Nothing

EachMessageThread = New Threading.Thread(AddressOf
clsEachMessage.ProcessMessage)
do while myThreadCount.ThreadCount >= 100
thread.sleep(100)
loop
MyThreadCount.Increment()
EachMessageThread.Start()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top