By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,952 Members | 1,592 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,952 IT Pros & Developers. It's quick & easy.

Thread takes up 100% CPU

P: n/a
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

May 22 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Yes, simply "sleep" for a few miliseconds on each iteration.
Thread.Sleep(100)
"fniles" <fn****@pfmail.com> escribió en el mensaje
news:ez**************@TK2MSFTNGP05.phx.gbl...
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

May 22 '06 #2

P: n/a
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...

--

"fniles" <fn****@pfmail.com> a écrit dans le message de news:
ez**************@TK2MSFTNGP05.phx.gbl...
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

May 22 '06 #3

P: n/a
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
'---------------------------------------

"Fred Hedges" <do******@spammuch.com> wrote in message
news:e4*******************@news.demon.co.uk...
Yes, simply "sleep" for a few miliseconds on each iteration.
Thread.Sleep(100)
"fniles" <fn****@pfmail.com> escribió en el mensaje
news:ez**************@TK2MSFTNGP05.phx.gbl...
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


May 22 '06 #4

P: n/a
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

"Patrice" <sc****@chez.com> wrote in message
news:O1**************@TK2MSFTNGP05.phx.gbl...
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...

--

"fniles" <fn****@pfmail.com> a écrit dans le message de news:
ez**************@TK2MSFTNGP05.phx.gbl...
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


May 22 '06 #5

P: n/a
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 :).

"fniles" <fn****@pfmail.com> wrote in message
news:uR**************@TK2MSFTNGP03.phx.gbl...
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
'---------------------------------------

"Fred Hedges" <do******@spammuch.com> wrote in message
news:e4*******************@news.demon.co.uk...
Yes, simply "sleep" for a few miliseconds on each iteration.
Thread.Sleep(100)
"fniles" <fn****@pfmail.com> escribió en el mensaje
news:ez**************@TK2MSFTNGP05.phx.gbl...
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



May 23 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.