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. 10 1479
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.
"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/>
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
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.
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.
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.
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.
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.
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Elbert Lev |
last post by:
Please correct me if I'm wrong.
Python (as I understand) uses reference counting to determine when to
delete the object. As soon as the object goes out of the scope it is
deleted. Python does...
|
by: ash |
last post by:
hi
does anyone has any experience with flyweight pattern with refernce
counting
i want to share objects between multiple clients and want to delete
the object from shared pool when the last...
|
by: David Poundall |
last post by:
I have a thread class and I want to be able to track its usage within
an application. FYI the class launches aplications in their own thread
when the 'launch' method is called.
That works OK
...
|
by: William Stacey [MVP] |
last post by:
Here is the link. If you find an issue or think of a feature, please post a
reply or send an email. Cheers!
http://www.mvptools.com/doco/csharp/semaphoredjikstra.htm
--
William Stacey, MVP
|
by: j |
last post by:
Hi,
I've been trying to do line/character counts on documents that are
being uploaded. As well as the "counting" I also have to remove
certain sections from the file.
So, firstly I was working...
|
by: aaronfude |
last post by:
Hi,
Please consider the following class (it's not really my class, but it's
a good example for my question):
class Vector {
int myN;
double *myX;
Vector(int n) : myN(n), myX(new double) { }...
|
by: blangela |
last post by:
If you had asked me 5 years ago about the future of C++, I would have
told you that its future was assured for many years to come. Recently,
I have been starting to wonder.
I have been teaching...
|
by: Chris Thomasson |
last post by:
Here are the pre-alpha code downloads:
http://appcore.home.comcast.net/vzoom/refcount/
(both C and C++ api here...)
Here is some pre-alpha documentation:
...
|
by: grabit |
last post by:
Hi Peoples
I have the following query to retrieve data from the db. 2 of the things it has to do is give me the date of the last post and a count of the post records in each of the categories. ...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |