Home All Groups Group Topic Archive Search About
Author
14 Feb 2006 7:51 PM
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.

Author
14 Feb 2006 9:03 PM
Shawn
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.
Author
14 Feb 2006 10:38 PM
Herfried K. Wagner [MVP]
"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/>
Author
15 Feb 2006 9:00 AM
Stephany Young
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.


Show quoteHide quote
"cj" <cj@nospam.nospam> wrote in message
news:OTNXBAaMGHA.1028@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.
Author
15 Feb 2006 2:50 PM
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:
Show quoteHide quote
> 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:OTNXBAaMGHA.1028@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.
>
>
Author
15 Feb 2006 9:47 PM
Stephany Young
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.


Show quoteHide quote
"cj" <cj@nospam.nospam> wrote in message
news:eZHl58jMGHA.3860@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:OTNXBAaMGHA.1028@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.
>>
Author
16 Feb 2006 1:24 PM
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:
Show quoteHide quote
> 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:eZHl58jMGHA.3860@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:OTNXBAaMGHA.1028@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.
>
Author
20 Feb 2006 9:46 AM
TerryFei
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: <OTNXBAaMGHA.1***@TK2MSFTNGP11.phx.gbl>
<Oi0KA5gMGHA.3***@TK2MSFTNGP11.phx.gbl>
<eZHl58jMGHA.3***@TK2MSFTNGP12.phx.gbl>
<uZ6ghlnMGHA.1***@TK2MSFTNGP12.phx.gbl>
Show quoteHide quote
>In-Reply-To: <uZ6ghlnMGHA.1***@TK2MSFTNGP12.phx.gbl>
>Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>Content-Transfer-Encoding: 7bit
>Message-ID: <uQVdgxvMGHA.3***@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!TK2MSFTNGP10.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:eZHl58jMGHA.3860@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:OTNXBAaMGHA.1028@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.
>>
>
Author
20 Feb 2006 1:04 PM
cj
The project is going fine.  I'm using Stephany Young's code.

TerryFei wrote:
Show quoteHide quote
> 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: <OTNXBAaMGHA.1***@TK2MSFTNGP11.phx.gbl>
> <Oi0KA5gMGHA.3***@TK2MSFTNGP11.phx.gbl>
> <eZHl58jMGHA.3***@TK2MSFTNGP12.phx.gbl>
> <uZ6ghlnMGHA.1***@TK2MSFTNGP12.phx.gbl>
>> In-Reply-To: <uZ6ghlnMGHA.1***@TK2MSFTNGP12.phx.gbl>
>> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>> Content-Transfer-Encoding: 7bit
>> Message-ID: <uQVdgxvMGHA.3***@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!TK2MSFTNGP10.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:eZHl58jMGHA.3860@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:OTNXBAaMGHA.1028@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.
>
Author
21 Feb 2006 3:19 AM
TerryFei
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: <OTNXBAaMGHA.1***@TK2MSFTNGP11.phx.gbl>
<Oi0KA5gMGHA.3***@TK2MSFTNGP11.phx.gbl>
<eZHl58jMGHA.3***@TK2MSFTNGP12.phx.gbl>
<uZ6ghlnMGHA.1***@TK2MSFTNGP12.phx.gbl>
<uQVdgxvMGHA.3***@TK2MSFTNGP10.phx.gbl>
<M3APEKgNGHA.3***@TK2MSFTNGXA01.phx.gbl>
Show quoteHide quote
>In-Reply-To: <M3APEKgNGHA.3***@TK2MSFTNGXA01.phx.gbl>
>Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>Content-Transfer-Encoding: 7bit
>Message-ID: <ugdWj4hNGHA.3***@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!TK2MSFTNGP15.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: <OTNXBAaMGHA.1***@TK2MSFTNGP11.phx.gbl>
>> <Oi0KA5gMGHA.3***@TK2MSFTNGP11.phx.gbl>
>> <eZHl58jMGHA.3***@TK2MSFTNGP12.phx.gbl>
>> <uZ6ghlnMGHA.1***@TK2MSFTNGP12.phx.gbl>
>>> In-Reply-To: <uZ6ghlnMGHA.1***@TK2MSFTNGP12.phx.gbl>
>>> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>>> Content-Transfer-Encoding: 7bit
>>> Message-ID: <uQVdgxvMGHA.3***@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!TK2MSFTNGP10.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:eZHl58jMGHA.3860@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:OTNXBAaMGHA.1028@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.
>>
>
Author
16 Feb 2006 10:09 PM
José_Manuel_Agüero
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.


Show quoteHide quote
"Stephany Young" <noone@localhost> escribió en el mensaje news:Oi0KA5gMGHA.3144@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:OTNXBAaMGHA.1028@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.