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
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
Case CommandStatus.AckReceived
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
reloader.Close()
Else
reloader.EndConnect(ar)
End If
wh.Close()
If Not reloader.Connected Then
reloader.Close()
End If
Case CommandStatus.ExpectingAck
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
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
Thread.Sleep(500)
End While
End Sub
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!