计算线程 [英] Counting threads

查看:73
本文介绍了计算线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个TCP / IP服务器应用程序,它将同时具有多个

连接。主线程侦听新连接并启动一个

线程来处理每个请求的连接。这些是短暂的

线程获取请求并返回一个回复并结束。


主线程可以告诉我当前有多少连接线程/>
在任何给定时间运行?我想在主窗体上有一个标签,以便

显示服务器当前正在服务的连接数。也许我会在表单上放一个计时器,每5秒左右更新一次计数。

解决方案

我用我的一个程序做了类似的事情。


我做的是在线程启动时添加到全局集合中并且

从集合中删除线程结束。这就是为什么我可以只用

来查询集合的数量,看看目前有多少线程正在服务。


" CJ" < cj@nospam.nospam> schrieb:

我正在编写一个TCP / IP服务器应用程序,它将具有许多同时的连接。主线程侦听新连接并启动一个
线程来处理每个请求的连接。这些是短命的线程
获取请求并返回回复并结束。




除了其他回复,您可能需要查看

''ThreadPool''班级。


-

MS Herfried K. Wagner

MVP< URL:http://dotnet.mvps.org/>

VB< URL:http://classicvb.org/petition/>


我认为它比其他人说的要简单。


如果你创建了一个小类:


公共类MyThreadCount


私有共享m_lock作为新对象


私有共享m_threadcount为Int32 = 0


公共共享子增量()


Synclock(m_lock)

m_threadcount + = 1

结束时钟


结束子


公共共享子减量()


Synclock(m_lock )

m_threadcount - = 1

End Syncl ock


结束子


公共共享子重置()


Synclock(m_lock)

m_threadcount = 0

结束时钟


结束子


公共共享ReadOnly属性ThreadCount()As Int32


获取

Dim _count为Int32

Synclock(m_lock)

_count = m_threadcount

结束时钟

返回_count

结束获取


结束物业

结束班级


在每个连接处理线程中调用

MyThreadCount.Increment作为第一个语句线程和调用

MyThreadCount.Decrement作为线程中的最后一个调用。


Sub ConnectionHandlerThread()


MyThreadCount.Increment


尝试


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 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


这篇关于计算线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆