本文介绍了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!

推荐答案


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





感谢任何帮助!



Any help is appreciated!



这篇关于Windows服务中TCP过多的CPU利用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 06:03