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.I ncrement()
When it ends it decrements the counter:
MyThreadCount.D ecrement()
The main program has a timer that looks at MyThreadCount.T hreadCount 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.
fniles wrote:
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