Windows服务中TCP过多的CPU利用率

编程入门 行业动态 更新时间:2024-10-24 16:27:59
本文介绍了Windows服务中TCP过多的CPU利用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好, 我有两个线程连接通过不同端口与服务器通信,如果服务器没有准备好,它会暂停一段时间(thread.sleep)并重试。 连接尝试是在while循环中,当第一个线程启动时,CPU利用率约为50%,当第二个线程启动时CPU充分利用并且机器上的所有内容都开始崩溃,挂起,...等 这是一个Windows服务,它与主服务器有主连接,主连接有一个接收事务的监听器线程,辅助连接用于请求事务重新加载表单服务器,当重新加载线程启动时,主连接仍处于活动状态,两个连接都有不同的端口号,实际上我有2个主连接和2个辅助连接(不同的端口),因为我在每个端口上收到不同类型的事务。 br /> 这个子程序启动重装线程。

Hello, I have 2 threads connecting communicating with the server through different ports, if the server is not ready, it''s gonna pause for a while (thread.sleep) and retry. The connection attempts is in a while loop, when the first thread starts, the CPU is about 50% utilized, when the second thread starts the CPU is fully utilized and everything on the machine starts to crash, hang, ..., etc This is a Windows Service, it has primary connection with the main server, the primary connection has a listener thread that receives transactions, the secondary connection is for requesting transactions reload form the server, when the reload thread starts, the main connection is still active, both connections have different port number, actually I have 2 main connections and 2 secondary connections (different ports), because I receive different type of transactions on each port. This subroutine starts the reload thread.

Private Sub startRealodThread() If Not isReloadThreadWorking Then IsProcessingReloadQueue = True Try ResponseMonitorService.wcfService.setReloading(True, myType) Me.selfReloading = True Dim ReloadThread As New Thread(AddressOf requestReload) ReloadThread.Name = "ReloadThread" & "_" & strMyType ReloadThread.SetApartmentState(Threading.ApartmentState.STA) ReloadThread.Start() RESET_STATUS = CommandStatus.NoCommand Catch ex As Exception ' Write log End Try End If End Sub

重装线程:

The reload thread:

Private Sub requestReload() isReloadThreadWorking = True Dim ResetCommand As String = STX & "\/RESET" & ETX Dim RetryCount As Integer = 0 Dim strData As String = "" Dim waiting As Boolean = True, sendACK As Boolean Dim reloader As New TcpClient Dim ConnCount As Integer = 1 Dim reloadStream As NetworkStream Dim done As Boolean = False Dim wrongAckCount As Integer = 0 On Error Resume Next While Not reloader.Connected If ExitReloadThread Then Me.selfReloading = False Me.needsReload = True reloadStream.Close() reloader.Close() ExitReloadThread = False isReloadThreadWorking = False Exit Sub End If Select Case RESET_STATUS Case CommandStatus.AboutToSend ' Write log Case CommandStatus.AckReceived ' Write Log Case CommandStatus.AckVerified ConnCount += 1 Dim ar As System.IAsyncResult = reloader.BeginConnect(Me.ip, Val(Me.port) + 20000, Nothing, Nothing) Dim wh As WaitHandle = ar.AsyncWaitHandle If Not ar.AsyncWaitHandle.WaitOne(1500, False) Then ' The logic to control when the connection timed out reloader.Close() 'Throw New TimeoutException() Else ' The logic to control when the connection succeeds. reloader.EndConnect(ar) End If wh.Close() If Not reloader.Connected Then reloader.Close() End If Case CommandStatus.ExpectingAck 'Write log Case CommandStatus.NoCommand RESET_STATUS = CommandStatus.Preparing ResponseMonitorService.wcfService.setReloading(True, myType) Me.selfReloading = True ResetTimeOutTimer.Start() Me.sendData(ResetCommand, Me.oLogEntity_Reload, True) Case CommandStatus.Preparing ' Write Log Case CommandStatus.Retry RetryCount += 1 If RetryCount = 3 Then ExitReloadThread = True End If RESET_STATUS = CommandStatus.NoCommand Case CommandStatus.SendFailed RESET_STATUS = CommandStatus.Retry Case CommandStatus.TimeOut ResetTimeOutTimer.Stop() RESET_STATUS = CommandStatus.Retry Case CommandStatus.WrongAck If wrongAckCount = 3 Then ExitReloadThread = True Else wrongAckCount += 1 RESET_STATUS = CommandStatus.ExpectingAck End If End Select ' Pause for 500 millisecond to fix the CPU utilization problem (Didn't work) Thread.Sleep(500) End While ' here we read and process the transactions ... ' ' ' ' End Sub

请帮帮我找到问题,我知道它是在while循环中,它过度利用了CPU,我使用了thread.sleep但是它没有工作! 谢谢 ****更新**** 我发现第一个线程连接没有问题,当第二个线程尝试连接时,CPU被100%使用。 它完全卡在了

Please help me to find the problem, I know it''s in the while loop, it''s excessively utilizing the CPU, I used thread.sleep but it didn''t work! Thanks **** Update **** I found that the first thread gets connected without problems, when the second thread attempts to connect, then the CPU is 100% used. It is stuck exactly on

If Not ar.AsyncWaitHandle.WaitOne(1500, False) Then

感谢任何帮助!

Any help is appreciated!

推荐答案

1)使用你r调试器,单步执行固定CPU的代码。找出实际上是什么电话问题。只需添加一个随机睡眠命令就像在2英寸的伤口上放一个创可贴。您似乎已经解决了问题,但它无法治愈根本问题。 2)一旦找到代码的问题行,请调查它们并使它们不固定中央处理器。发布存在根问题的一些代码,我们可以看看是否可以帮助您解决问题。 1) Using your debugger, step through the code which is pinning your CPU. Find out what calls are actually the problem. Just adding a random sleep command in is like putting a band-aid on a 2 inch gash. You may appear to have solved the problem, but it won''t heal the root issue. 2) Once you find the problem line(s) of code, investigate them and make them not pin the CPU. Post some code surrounding where the root problem exists and we can see if we can help you solve your issue.

****更新**** 我发现第一个线程连接没有问题,当第二个线程尝试连接时,则CPU被100%使用。 它完全卡在 **** Update **** I found that the first thread gets connected without problems, when the second thread attempts to connect, then the CPU is 100% used. It is stuck exactly on If Not ar.AsyncWaitHandle.WaitOne(1500, False) Then

感谢任何帮助!

Any help is appreciated!

我发现有一个紧凑的循环!! I found that there was a tight loop!!

更多推荐

Windows服务中TCP过多的CPU利用率

本文发布于:2023-11-03 20:09:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555976.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:利用率   Windows   TCP   CPU

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!