Thread takes up 100% CPU

F

fniles

I am having problem with thread. I have a Session class with public string

variable (called Message) that I set from my Main program. In the session

class it checks for the value of Message while inside it's "read loop"

waiting for data from the client. In the main program, I check to see if

the Message member has been cleared before changing its value, that way you
know it has been written by the

session thread. The idea would be that if a thread hasn't written its data
to the client yet, move on to the next thread and then go back and check
again on those threads that we're ready in the previous pass.

But, by doing this, the CPU usage is 100%.

Is there any way to do this without driving CPU usage to 100% ?

Thanks a lot.



'****************Main program*************************
Imports System.Threading
Private Structure SessionInfo
Dim Session As SessionClass
Dim Thread As Threading.Thread
End Structure
Private Client(0) As SessionInfo
Private LastClient As Integer
Private m_cnt As Long

Private Sub TestButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TestButton.Click
Dim oSession As SessionClass
Dim oThread As Threading.Thread
Static nSession As Long

nSession = nSession + 1
oSession = New SessionClass
oSession.Server = Me
oThread = New Threading.Thread(AddressOf oSession.ThreadMain)
oThread.ApartmentState = ApartmentState.STA
oThread.Name = "Session" & CLng(nSession)
oThread.Start()
Timer1.Enabled = True
End Sub

Sub Test()
Dim arr() As Long

Dim lMax As Long

Dim bLoop As Boolean

Dim x As Long
Dim nIndex As Integer

Monitor.Enter(Client)
For nIndex = 0 To LastClient - 1
If Not Client(nIndex).Session Is Nothing Then
If Client(nIndex).Session.Message = "" Then
Client(nIndex).Session.Message = m_cnt
Else
lMax = lMax + 1
ReDim Preserve arr(lMax)
arr(lMax) = nIndex
End If
End If
Next
If lMax > 0 Then bLoop = True Else bLoop = False

'--------------------------------------->>>>>the following takes up to 100%
CPU<<<<<<--------------------------------------------------------
Do While bLoop
bLoop = False
For x = 1 To lMax
nIndex = arr(x)
If nIndex > -1 Then
If Not Client(nIndex).Session Is Nothing Then
If Client(nIndex).Session.Message = "" Then
Client(nIndex).Session.Message = m_cnt
arr(x) = -1
Else
bLoop = True
End If
End If
End If
Next
Loop
Monitor.Exit(Client)
End Sub
'---------------------------------------

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
m_cnt = m_cnt + 1
Test()
End Sub

'*************Session.vb********************
Imports System.Threading
Public Class SessionClass
Public Server As ServerForm
Public Message As String
Private Terminated As Boolean

Public Sub ThreadMain()
Dim nThreadId As Integer
Dim strThreadName As String
Dim strBuffer As String
Dim nResult As Integer
Dim sw As StreamWriter

If Server.RegisterClient(Me, Thread.CurrentThread()) = False Then
Exit Sub
End If

Do While Not Terminated
If Len(Me.Message) > 0 Then
Me.Message = ""
End If
Loop
Server.UnregisterClient(Me)
Exit Sub

End Sub

Public Sub Terminate()
Terminated = True
End Sub
 
F

fniles

Thanks for your quick reply.
Did you mean like the following ?
I tried it, and the CPU usage when down, but I see delay in the client
receiving the Message.
I tried thread.sleep(0), and the CPU usage when up.
Am I doing things correctly ? I simply want to send messages to the clients'
thread, and the clients need to receive all the messages I am sending from
the main program, and receive them on time, not delayed.
Thanks.

'--------------------------------------->>>>>the following takes up to 100%
CPU<<<<<<--------------------------------------------------------
Do While bLoop
thread.sleep(100) '---->>> LIKE this ?
bLoop = False
For x = 1 To lMax
nIndex = arr(x)
If nIndex > -1 Then
If Not Client(nIndex).Session Is Nothing Then
If Client(nIndex).Session.Message = "" Then
Client(nIndex).Session.Message = m_cnt
arr(x) = -1
Else
bLoop = True
End If
End If
End If
Next
Loop
Monitor.Exit(Client)
End Sub
'---------------------------------------
 
P

Patrice

What are you trying to do ? I don't see where Client(nIndex).Session is
initialized (I just see oSession being inialized). IMO you are stuck in a
tight loop so...

--
 
F

fniles

Thank you.
In my original program what I would like to do is the following:
I have a TCP component in the main program and it is connected to an IP
address where it gets data.
As it receives data, I would like to send that data right away to all the
clients connected to the main program.
Each client connection is a thread (session class).
I found that the client is missing some data that the main program sends.
Therefore, in the main program, I check to see if
the Message member has been cleared before changing its value, that way I
know it has been written by the session thread.
So, if a thread hasn't written its data to the client yet, move on to the
next thread and then go back and check again on those threads that we skip
in the previous pass.

To simplify the program, I wrote a simple program using a timer (instead of
receiving data from a TCP component), and every milisecond it sets the
Message variable from the session class.

This is the complete code from the simplyfied program:
'****************Main program*************************
Imports System.Threading
Private Structure SessionInfo
Dim Session As SessionClass
Dim Thread As Threading.Thread
End Structure
Private Client(0) As SessionInfo
Private LastClient As Integer
Private m_cnt As Long

Public Function RegisterClient(ByRef Session As SessionClass, ByRef Thread
As Threading.Thread) As Boolean
Dim nIndex As Integer
Dim nMaxClients As Integer

RegisterClient = False
nMaxClients = Val(MaxClients.Text)
Monitor.Enter(Client)
For nIndex = 0 To LastClient - 1
If Client(nIndex).Session Is Nothing Then
Client(nIndex).Session = Session
Client(nIndex).Thread = Thread
RegisterClient = True
Monitor.Exit(Client)
Exit Function
End If
Next

ReDim Preserve Client(LastClient)
Client(LastClient).Session = Session
Client(LastClient).Thread = Thread
LastClient = LastClient + 1
RegisterClient = True
Monitor.Exit(Client)
End Function

Public Function UnregisterClient(ByRef Session As SessionClass) As Boolean
Dim nIndex As Integer

UnregisterClient = False
Monitor.Enter(Client)
For nIndex = 0 To LastClient - 1
If Client(nIndex).Session Is Session Then
Client(nIndex).Session = Nothing
Client(nIndex).Thread = Nothing
UnregisterClient = True
Exit For
End If
Next
Monitor.Exit(Client)
End Function

Private Sub TestButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TestButton.Click
Dim oSession As SessionClass
Dim oThread As Threading.Thread
Static nSession As Long

nSession = nSession + 1
oSession = New SessionClass
oSession.Server = Me
oThread = New Threading.Thread(AddressOf oSession.ThreadMain)
oThread.ApartmentState = ApartmentState.STA
oThread.Name = "Session" & CLng(nSession)
oThread.Start()
Timer1.Enabled = True
End Sub

Sub Test()
Dim arr() As Long
Dim lMax As Long
Dim bLoop As Boolean
Dim x As Long
Dim nIndex As Integer

Monitor.Enter(Client)
For nIndex = 0 To LastClient - 1
If Not Client(nIndex).Session Is Nothing Then
If Client(nIndex).Session.Message = "" Then
Client(nIndex).Session.Message = m_cnt
Else
lMax = lMax + 1
ReDim Preserve arr(lMax)
arr(lMax) = nIndex
End If
End If
Next
If lMax > 0 Then bLoop = True Else bLoop = False

'--------------------------------------->>>>>the following takes up to 100%
CPU<<<<<<-------------------------------------------------------
Do While bLoop
bLoop = False
For x = 1 To lMax
nIndex = arr(x)
If nIndex > -1 Then
If Not Client(nIndex).Session Is Nothing Then
If Client(nIndex).Session.Message = "" Then
Client(nIndex).Session.Message = m_cnt
arr(x) = -1
Else
bLoop = True
End If
End If
End If
Next
Loop
Monitor.Exit(Client)
End Sub
'---------------------------------------

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
m_cnt = m_cnt + 1
Test()
End Sub

'*************Session.vb********************
Imports System.Threading
Public Class SessionClass
Public Server As ServerForm
Public Message As String
Private Terminated As Boolean

Public Sub ThreadMain()
Dim nThreadId As Integer
Dim strThreadName As String
Dim strBuffer As String
Dim nResult As Integer
Dim sw As StreamWriter

If Server.RegisterClient(Me, Thread.CurrentThread()) = False Then
Exit Sub
End If

Do While Not Terminated
If Len(Me.Message) > 0 Then
Me.Message = ""
End If
Loop
Server.UnregisterClient(Me)
Exit Sub

End Sub

Public Sub Terminate()
Terminated = True
End Sub
 
F

Fred Hedges

You can be more sophisticated about it and sleep if you haven't sent a
message for n seconds, or iterations or whatever. But if you want to give
up some CPU time, you have to sleep :).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top