By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,582 Members | 1,720 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,582 IT Pros & Developers. It's quick & easy.

Counting threads

P: n/a
cj
I'm writing a TCP/IP server app that will have many simultaneous
connections. The main thread listens for new connections and starts a
thread to handle each requested connection. These are short lived
threads that get a request and return a reply and end.

Can the main thread tell me how many connection threads are currently
running at any given time? I'd like to have a label on the main form to
show how many connections the server is currently servicing. Maybe I'd
put a timer on the form and every 5 seconds or so update the count.
Feb 14 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
I do something similar with one of my programs.

What I do is add to a global collection when a thread is started and
remove from the collection when the thread ends. This why I can just
query the count of the collection to see how many threads are currently
being serviced.

Feb 14 '06 #2

P: n/a
"cj" <cj@nospam.nospam> schrieb:
I'm writing a TCP/IP server app that will have many simultaneous
connections. The main thread listens for new connections and starts a
thread to handle each requested connection. These are short lived threads
that get a request and return a reply and end.


In addition to the other reply you may want to take a look at the
'ThreadPool' class.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Feb 14 '06 #3

P: n/a
I think it's a bit simpler than the others indicated.

If you create a small class thus:

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 Sub Reset()

Synclock(m_lock)
m_threadcount = 0
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

In each of your connection handling threads make a call to
MyThreadCount.Increment as the first statement in the thread and a call to
MyThreadCount.Decrement as the last call in the thread.

Sub ConnectionHandlerThread()

MyThreadCount.Increment

Try
Feb 15 '06 #4

P: n/a
cj
I understand your code but I do have one question. As written couldn't
one thread be incrementing m_threadcount at the very instant that
another is decrementing it? Wouldn't this be a problem too? If so I'd
think it'd need to be one sub that changes the value of m_threadcount
and the operator be passed as a variable to it. What do you think?
Stephany Young wrote:
I think it's a bit simpler than the others indicated.

If you create a small class thus:

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 Sub Reset()

Synclock(m_lock)
m_threadcount = 0
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

In each of your connection handling threads make a call to
MyThreadCount.Increment as the first statement in the thread and a call to
MyThreadCount.Decrement as the last call in the thread.

Sub ConnectionHandlerThread()

MyThreadCount.Increment

Try
.
.
.
Catch ...
.
.
.
Finally
' Any other cleanup code
MyThreadCount.Decrement
End Try

End Sub

Putting the thread code in a Try...Catch...Finally and having the call to
MyThreadCount.Decrement as the last line in the Finally section ensures that
the thread cannot terminate without calling MyThreadCount.Decrement.

When you want to know how many threads are active simply interrogate the
value of the MyThreadCount.ThreadCount property.

The use of Synclock ensures that only 1 thread at a time can update the
counter and that no thread can update the counter while it is being
interrogated.

Note that the value of the ThreadCount property represents the number of
active threads at the point in time thate the _count= m_threadcount
statement is executed.

By the time you see the result displayed more threads may have started
and/or finished.

Because it is likely to be useful, I have included a Reset method which is
self explanatory.

I am sure that you will be able to tailor the example to suit you own needs
and make it more robust.
"cj" <cj@nospam.nospam> wrote in message
news:OT**************@TK2MSFTNGP11.phx.gbl...
I'm writing a TCP/IP server app that will have many simultaneous
connections. The main thread listens for new connections and starts a
thread to handle each requested connection. These are short lived threads
that get a request and return a reply and end.

Can the main thread tell me how many connection threads are currently
running at any given time? I'd like to have a label on the main form to
show how many connections the server is currently servicing. Maybe I'd
put a timer on the form and every 5 seconds or so update the count.


Feb 15 '06 #5

P: n/a
I covered that in the paragraph that starts with 'The use of Synclock'.

If it will make it easier to understand, then take out all the 'Shared'
keywords, and instantiate 1 (and 1 only) instance of type MyThreadCount as a
public variable:

Public MyThreadCount As New MyThreadCount

Thus, is can be seen that all threads are using the same instance.

When one thread calls MyThreadCount.Increment, it acquires a lock on the
m_lock object. Any calls by other threads before that lock is released will
queue up.

When the first thread releases the lock on the m_lock object, the next
thread in the queue will acquire the lock and so on ad infinitum.

Yes, it is true that waiting for a lock will block the calling thread but we
are only talking about nanoseconds.
"cj" <cj@nospam.nospam> wrote in message
news:eZ**************@TK2MSFTNGP12.phx.gbl...
I understand your code but I do have one question. As written couldn't one
thread be incrementing m_threadcount at the very instant that another is
decrementing it? Wouldn't this be a problem too? If so I'd think it'd
need to be one sub that changes the value of m_threadcount and the operator
be passed as a variable to it. What do you think?
Stephany Young wrote:
I think it's a bit simpler than the others indicated.

If you create a small class thus:

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 Sub Reset()

Synclock(m_lock)
m_threadcount = 0
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

In each of your connection handling threads make a call to
MyThreadCount.Increment as the first statement in the thread and a call
to MyThreadCount.Decrement as the last call in the thread.

Sub ConnectionHandlerThread()

MyThreadCount.Increment

Try
.
.
.
Catch ...
.
.
.
Finally
' Any other cleanup code
MyThreadCount.Decrement
End Try

End Sub

Putting the thread code in a Try...Catch...Finally and having the call to
MyThreadCount.Decrement as the last line in the Finally section ensures
that the thread cannot terminate without calling MyThreadCount.Decrement.

When you want to know how many threads are active simply interrogate the
value of the MyThreadCount.ThreadCount property.

The use of Synclock ensures that only 1 thread at a time can update the
counter and that no thread can update the counter while it is being
interrogated.

Note that the value of the ThreadCount property represents the number of
active threads at the point in time thate the _count= m_threadcount
statement is executed.

By the time you see the result displayed more threads may have started
and/or finished.

Because it is likely to be useful, I have included a Reset method which
is self explanatory.

I am sure that you will be able to tailor the example to suit you own
needs and make it more robust.
"cj" <cj@nospam.nospam> wrote in message
news:OT**************@TK2MSFTNGP11.phx.gbl...
I'm writing a TCP/IP server app that will have many simultaneous
connections. The main thread listens for new connections and starts a
thread to handle each requested connection. These are short lived
threads that get a request and return a reply and end.

Can the main thread tell me how many connection threads are currently
running at any given time? I'd like to have a label on the main form to
show how many connections the server is currently servicing. Maybe I'd
put a timer on the form and every 5 seconds or so update the count.


Feb 15 '06 #6

P: n/a
cj
Ok, I did read your post and understood that synclock would allow one
thread at a time but was unclear as to whether that meant one thread at
a time across all subs. I see now it does. Thanks
Stephany Young wrote:
I covered that in the paragraph that starts with 'The use of Synclock'.

If it will make it easier to understand, then take out all the 'Shared'
keywords, and instantiate 1 (and 1 only) instance of type MyThreadCount as a
public variable:

Public MyThreadCount As New MyThreadCount

Thus, is can be seen that all threads are using the same instance.

When one thread calls MyThreadCount.Increment, it acquires a lock on the
m_lock object. Any calls by other threads before that lock is released will
queue up.

When the first thread releases the lock on the m_lock object, the next
thread in the queue will acquire the lock and so on ad infinitum.

Yes, it is true that waiting for a lock will block the calling thread but we
are only talking about nanoseconds.
"cj" <cj@nospam.nospam> wrote in message
news:eZ**************@TK2MSFTNGP12.phx.gbl...
I understand your code but I do have one question. As written couldn't one
thread be incrementing m_threadcount at the very instant that another is
decrementing it? Wouldn't this be a problem too? If so I'd think it'd
need to be one sub that changes the value of m_threadcount and the operator
be passed as a variable to it. What do you think?
Stephany Young wrote:
I think it's a bit simpler than the others indicated.

If you create a small class thus:

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 Sub Reset()

Synclock(m_lock)
m_threadcount = 0
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

In each of your connection handling threads make a call to
MyThreadCount.Increment as the first statement in the thread and a call
to MyThreadCount.Decrement as the last call in the thread.

Sub ConnectionHandlerThread()

MyThreadCount.Increment

Try
.
.
.
Catch ...
.
.
.
Finally
' Any other cleanup code
MyThreadCount.Decrement
End Try

End Sub

Putting the thread code in a Try...Catch...Finally and having the call to
MyThreadCount.Decrement as the last line in the Finally section ensures
that the thread cannot terminate without calling MyThreadCount.Decrement.

When you want to know how many threads are active simply interrogate the
value of the MyThreadCount.ThreadCount property.

The use of Synclock ensures that only 1 thread at a time can update the
counter and that no thread can update the counter while it is being
interrogated.

Note that the value of the ThreadCount property represents the number of
active threads at the point in time thate the _count= m_threadcount
statement is executed.

By the time you see the result displayed more threads may have started
and/or finished.

Because it is likely to be useful, I have included a Reset method which
is self explanatory.

I am sure that you will be able to tailor the example to suit you own
needs and make it more robust.
"cj" <cj@nospam.nospam> wrote in message
news:OT**************@TK2MSFTNGP11.phx.gbl...
I'm writing a TCP/IP server app that will have many simultaneous
connections. The main thread listens for new connections and starts a
thread to handle each requested connection. These are short lived
threads that get a request and return a reply and end.

Can the main thread tell me how many connection threads are currently
running at any given time? I'd like to have a label on the main form to
show how many connections the server is currently servicing. Maybe I'd
put a timer on the form and every 5 seconds or so update the count.

Feb 16 '06 #7

P: n/a
Hi Stephany,

Your code is a good general purpose sample, but for this particular matter you don't need synclock.
As the variable you are modifying is an integer, you can use the interlocked class to increment and decrement the variable anywhere without acquiring and releasing locks.
As the access to an integer is an atomic operation, you can safely read the value at any moment.

Regards.
"Stephany Young" <noone@localhost> escribió en el mensaje news:Oi**************@TK2MSFTNGP11.phx.gbl...
|I think it's a bit simpler than the others indicated.
|
| If you create a small class thus:
|
| 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 Sub Reset()
|
| Synclock(m_lock)
| m_threadcount = 0
| 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
|
| In each of your connection handling threads make a call to
| MyThreadCount.Increment as the first statement in the thread and a call to
| MyThreadCount.Decrement as the last call in the thread.
|
| Sub ConnectionHandlerThread()
|
| MyThreadCount.Increment
|
| Try
| .
| .
| .
| Catch ...
| .
| .
| .
| Finally
| ' Any other cleanup code
| MyThreadCount.Decrement
| End Try
|
| End Sub
|
| Putting the thread code in a Try...Catch...Finally and having the call to
| MyThreadCount.Decrement as the last line in the Finally section ensures that
| the thread cannot terminate without calling MyThreadCount.Decrement.
|
| When you want to know how many threads are active simply interrogate the
| value of the MyThreadCount.ThreadCount property.
|
| The use of Synclock ensures that only 1 thread at a time can update the
| counter and that no thread can update the counter while it is being
| interrogated.
|
| Note that the value of the ThreadCount property represents the number of
| active threads at the point in time thate the _count= m_threadcount
| statement is executed.
|
| By the time you see the result displayed more threads may have started
| and/or finished.
|
| Because it is likely to be useful, I have included a Reset method which is
| self explanatory.
|
| I am sure that you will be able to tailor the example to suit you own needs
| and make it more robust.
|
|
| "cj" <cj@nospam.nospam> wrote in message
| news:OT**************@TK2MSFTNGP11.phx.gbl...
| > I'm writing a TCP/IP server app that will have many simultaneous
| > connections. The main thread listens for new connections and starts a
| > thread to handle each requested connection. These are short lived threads
| > that get a request and return a reply and end.
| >
| > Can the main thread tell me how many connection threads are currently
| > running at any given time? I'd like to have a label on the main form to
| > show how many connections the server is currently servicing. Maybe I'd
| > put a timer on the form and every 5 seconds or so update the count.

Feb 16 '06 #8

P: n/a
Hi cj,
Thanks for Stephany's reply!

I just wanted to check how things are going . If there is any question,
please feel free to join the community and we are here to support you at
your convenience. Thanks for your understanding!

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
Date: Thu, 16 Feb 2006 08:24:55 -0500
From: cj <cj@nospam.nospam>
User-Agent: Thunderbird 1.5 (Windows/20051201)
MIME-Version: 1.0
Subject: Re: Counting threads
References: <OT**************@TK2MSFTNGP11.phx.gbl> <Oi**************@TK2MSFTNGP11.phx.gbl>
<eZ**************@TK2MSFTNGP12.phx.gbl>
<uZ**************@TK2MSFTNGP12.phx.gbl>In-Reply-To: <uZ**************@TK2MSFTNGP12.phx.gbl>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <uQ**************@TK2MSFTNGP10.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: 208.254.170.98
Lines: 1
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP10.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.languages.vb:317979
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Ok, I did read your post and understood that synclock would allow one
thread at a time but was unclear as to whether that meant one thread at
a time across all subs. I see now it does. Thanks
Stephany Young wrote:
I covered that in the paragraph that starts with 'The use of Synclock'.

If it will make it easier to understand, then take out all the 'Shared'
keywords, and instantiate 1 (and 1 only) instance of type MyThreadCount as a public variable:

Public MyThreadCount As New MyThreadCount

Thus, is can be seen that all threads are using the same instance.

When one thread calls MyThreadCount.Increment, it acquires a lock on the
m_lock object. Any calls by other threads before that lock is released will queue up.

When the first thread releases the lock on the m_lock object, the next
thread in the queue will acquire the lock and so on ad infinitum.

Yes, it is true that waiting for a lock will block the calling thread but we are only talking about nanoseconds.
"cj" <cj@nospam.nospam> wrote in message
news:eZ**************@TK2MSFTNGP12.phx.gbl...
I understand your code but I do have one question. As written couldn't one thread be incrementing m_threadcount at the very instant that another is decrementing it? Wouldn't this be a problem too? If so I'd think it'd
need to be one sub that changes the value of m_threadcount and the operator be passed as a variable to it. What do you think?
Stephany Young wrote:
I think it's a bit simpler than the others indicated.

If you create a small class thus:

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 Sub Reset()

Synclock(m_lock)
m_threadcount = 0
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

In each of your connection handling threads make a call to
MyThreadCount.Increment as the first statement in the thread and a call to MyThreadCount.Decrement as the last call in the thread.

Sub ConnectionHandlerThread()

MyThreadCount.Increment

Try
.
.
.
Catch ...
.
.
.
Finally
' Any other cleanup code
MyThreadCount.Decrement
End Try

End Sub

Putting the thread code in a Try...Catch...Finally and having the call to MyThreadCount.Decrement as the last line in the Finally section ensures that the thread cannot terminate without calling MyThreadCount.Decrement.
When you want to know how many threads are active simply interrogate the value of the MyThreadCount.ThreadCount property.

The use of Synclock ensures that only 1 thread at a time can update the counter and that no thread can update the counter while it is being
interrogated.

Note that the value of the ThreadCount property represents the number of active threads at the point in time thate the _count= m_threadcount
statement is executed.

By the time you see the result displayed more threads may have started
and/or finished.

Because it is likely to be useful, I have included a Reset method which is self explanatory.

I am sure that you will be able to tailor the example to suit you own
needs and make it more robust.
"cj" <cj@nospam.nospam> wrote in message
news:OT**************@TK2MSFTNGP11.phx.gbl...
> I'm writing a TCP/IP server app that will have many simultaneous
> connections. The main thread listens for new connections and starts a> thread to handle each requested connection. These are short lived
> threads that get a request and return a reply and end.
>
> Can the main thread tell me how many connection threads are currently
> running at any given time? I'd like to have a label on the main form to> show how many connections the server is currently servicing. Maybe I'd> put a timer on the form and every 5 seconds or so update the count.


Feb 20 '06 #9

P: n/a
cj
The project is going fine. I'm using Stephany Young's code.

TerryFei wrote:
Hi cj,
Thanks for Stephany's reply!

I just wanted to check how things are going . If there is any question,
please feel free to join the community and we are here to support you at
your convenience. Thanks for your understanding!

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
Date: Thu, 16 Feb 2006 08:24:55 -0500
From: cj <cj@nospam.nospam>
User-Agent: Thunderbird 1.5 (Windows/20051201)
MIME-Version: 1.0
Subject: Re: Counting threads
References: <OT**************@TK2MSFTNGP11.phx.gbl>

<Oi**************@TK2MSFTNGP11.phx.gbl>
<eZ**************@TK2MSFTNGP12.phx.gbl>
<uZ**************@TK2MSFTNGP12.phx.gbl>
In-Reply-To: <uZ**************@TK2MSFTNGP12.phx.gbl>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <uQ**************@TK2MSFTNGP10.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: 208.254.170.98
Lines: 1
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP10.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.languages.vb:317979
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Ok, I did read your post and understood that synclock would allow one
thread at a time but was unclear as to whether that meant one thread at
a time across all subs. I see now it does. Thanks
Stephany Young wrote:
I covered that in the paragraph that starts with 'The use of Synclock'.

If it will make it easier to understand, then take out all the 'Shared'
keywords, and instantiate 1 (and 1 only) instance of type MyThreadCount as a public variable:

Public MyThreadCount As New MyThreadCount

Thus, is can be seen that all threads are using the same instance.

When one thread calls MyThreadCount.Increment, it acquires a lock on the
m_lock object. Any calls by other threads before that lock is released will queue up.

When the first thread releases the lock on the m_lock object, the next
thread in the queue will acquire the lock and so on ad infinitum.

Yes, it is true that waiting for a lock will block the calling thread but we are only talking about nanoseconds.
"cj" <cj@nospam.nospam> wrote in message
news:eZ**************@TK2MSFTNGP12.phx.gbl...
I understand your code but I do have one question. As written couldn't one thread be incrementing m_threadcount at the very instant that another is decrementing it? Wouldn't this be a problem too? If so I'd think it'd
need to be one sub that changes the value of m_threadcount and the operator be passed as a variable to it. What do you think?
Stephany Young wrote:
> I think it's a bit simpler than the others indicated.
>
> If you create a small class thus:
>
> 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 Sub Reset()
>
> Synclock(m_lock)
> m_threadcount = 0
> 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
>
> In each of your connection handling threads make a call to
> MyThreadCount.Increment as the first statement in the thread and a call> to MyThreadCount.Decrement as the last call in the thread.
>
> Sub ConnectionHandlerThread()
>
> MyThreadCount.Increment
>
> Try
> .
> .
> .
> Catch ...
> .
> .
> .
> Finally
> ' Any other cleanup code
> MyThreadCount.Decrement
> End Try
>
> End Sub
>
> Putting the thread code in a Try...Catch...Finally and having the call to> MyThreadCount.Decrement as the last line in the Finally section ensures> that the thread cannot terminate without calling MyThreadCount.Decrement.> When you want to know how many threads are active simply interrogate the> value of the MyThreadCount.ThreadCount property.
>
> The use of Synclock ensures that only 1 thread at a time can update the> counter and that no thread can update the counter while it is being
> interrogated.
>
> Note that the value of the ThreadCount property represents the number of> active threads at the point in time thate the _count= m_threadcount
> statement is executed.
>
> By the time you see the result displayed more threads may have started
> and/or finished.
>
> Because it is likely to be useful, I have included a Reset method which> is self explanatory.
>
> I am sure that you will be able to tailor the example to suit you own
> needs and make it more robust.
>
>
> "cj" <cj@nospam.nospam> wrote in message
> news:OT**************@TK2MSFTNGP11.phx.gbl...
>> I'm writing a TCP/IP server app that will have many simultaneous
>> connections. The main thread listens for new connections and starts a>> thread to handle each requested connection. These are short lived
>> threads that get a request and return a reply and end.
>>
>> Can the main thread tell me how many connection threads are currently
>> running at any given time? I'd like to have a label on the main form to>> show how many connections the server is currently servicing. Maybe I'd>> put a timer on the form and every 5 seconds or so update the count.

Feb 20 '06 #10

P: n/a
Hi cj,
I am glad to know that the problem is resolved now. Thanks for
participating the community!

Best Regards,

Terry Fei [MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security

--------------------
Date: Mon, 20 Feb 2006 08:04:08 -0500
From: cj <cj@nospam.nospam>
User-Agent: Thunderbird 1.5 (Windows/20051201)
MIME-Version: 1.0
Subject: Re: Counting threads
References: <OT**************@TK2MSFTNGP11.phx.gbl> <Oi**************@TK2MSFTNGP11.phx.gbl>
<eZ**************@TK2MSFTNGP12.phx.gbl>
<uZ**************@TK2MSFTNGP12.phx.gbl>
<uQ**************@TK2MSFTNGP10.phx.gbl>
<M3**************@TK2MSFTNGXA01.phx.gbl>In-Reply-To: <M3**************@TK2MSFTNGXA01.phx.gbl>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <ug**************@TK2MSFTNGP15.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: 208.254.170.98
Lines: 1
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP15.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.languages.vb:318522
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

The project is going fine. I'm using Stephany Young's code.

TerryFei wrote:
Hi cj,
Thanks for Stephany's reply!

I just wanted to check how things are going . If there is any question,
please feel free to join the community and we are here to support you at
your convenience. Thanks for your understanding!

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
Date: Thu, 16 Feb 2006 08:24:55 -0500
From: cj <cj@nospam.nospam>
User-Agent: Thunderbird 1.5 (Windows/20051201)
MIME-Version: 1.0
Subject: Re: Counting threads
References: <OT**************@TK2MSFTNGP11.phx.gbl>

<Oi**************@TK2MSFTNGP11.phx.gbl>
<eZ**************@TK2MSFTNGP12.phx.gbl>
<uZ**************@TK2MSFTNGP12.phx.gbl>
In-Reply-To: <uZ**************@TK2MSFTNGP12.phx.gbl>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <uQ**************@TK2MSFTNGP10.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: 208.254.170.98
Lines: 1
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP10.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.languages.vb:317979
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Ok, I did read your post and understood that synclock would allow one
thread at a time but was unclear as to whether that meant one thread at
a time across all subs. I see now it does. Thanks
Stephany Young wrote:
I covered that in the paragraph that starts with 'The use of Synclock'.

If it will make it easier to understand, then take out all the 'Shared' keywords, and instantiate 1 (and 1 only) instance of type MyThreadCount
as a
public variable:

Public MyThreadCount As New MyThreadCount

Thus, is can be seen that all threads are using the same instance.

When one thread calls MyThreadCount.Increment, it acquires a lock on
the m_lock object. Any calls by other threads before that lock is released

will
queue up.

When the first thread releases the lock on the m_lock object, the next
thread in the queue will acquire the lock and so on ad infinitum.

Yes, it is true that waiting for a lock will block the calling thread

but we
are only talking about nanoseconds.
"cj" <cj@nospam.nospam> wrote in message
news:eZ**************@TK2MSFTNGP12.phx.gbl...
> I understand your code but I do have one question. As written couldn't one
> thread be incrementing m_threadcount at the very instant that another

is
> decrementing it? Wouldn't this be a problem too? If so I'd think
it'd> need to be one sub that changes the value of m_threadcount and the

operator
> be passed as a variable to it. What do you think?
>
>
> Stephany Young wrote:
>> I think it's a bit simpler than the others indicated.
>>
>> If you create a small class thus:
>>
>> 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 Sub Reset()
>>
>> Synclock(m_lock)
>> m_threadcount = 0
>> 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
>>
>> In each of your connection handling threads make a call to
>> MyThreadCount.Increment as the first statement in the thread and a

call
>> to MyThreadCount.Decrement as the last call in the thread.
>>
>> Sub ConnectionHandlerThread()
>>
>> MyThreadCount.Increment
>>
>> Try
>> .
>> .
>> .
>> Catch ...
>> .
>> .
>> .
>> Finally
>> ' Any other cleanup code
>> MyThreadCount.Decrement
>> End Try
>>
>> End Sub
>>
>> Putting the thread code in a Try...Catch...Finally and having the call to
>> MyThreadCount.Decrement as the last line in the Finally section

ensures
>> that the thread cannot terminate without calling

MyThreadCount.Decrement.
>> When you want to know how many threads are active simply interrogate

the
>> value of the MyThreadCount.ThreadCount property.
>>
>> The use of Synclock ensures that only 1 thread at a time can update

the
>> counter and that no thread can update the counter while it is being
>> interrogated.
>>
>> Note that the value of the ThreadCount property represents the
number of
>> active threads at the point in time thate the _count= m_threadcount
>> statement is executed.
>>
>> By the time you see the result displayed more threads may have
started>> and/or finished.
>>
>> Because it is likely to be useful, I have included a Reset method

which
>> is self explanatory.
>>
>> I am sure that you will be able to tailor the example to suit you own>> needs and make it more robust.
>>
>>
>> "cj" <cj@nospam.nospam> wrote in message
>> news:OT**************@TK2MSFTNGP11.phx.gbl...
>>> I'm writing a TCP/IP server app that will have many simultaneous
>>> connections. The main thread listens for new connections and starts a
>>> thread to handle each requested connection. These are short lived
>>> threads that get a request and return a reply and end.
>>>
>>> Can the main thread tell me how many connection threads are
currently>>> running at any given time? I'd like to have a label on the main

form to
>>> show how many connections the server is currently servicing. Maybe

I'd
>>> put a timer on the form and every 5 seconds or so update the count.


Feb 21 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.