473,729 Members | 2,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Thread takes up 100% CPU

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.Threadin g
Private Structure SessionInfo
Dim Session As SessionClass
Dim Thread As Threading.Threa d
End Structure
Private Client(0) As SessionInfo
Private LastClient As Integer
Private m_cnt As Long

Private Sub TestButton_Clic k(ByVal sender As Object, ByVal e As
System.EventArg s) Handles TestButton.Clic k
Dim oSession As SessionClass
Dim oThread As Threading.Threa d
Static nSession As Long

nSession = nSession + 1
oSession = New SessionClass
oSession.Server = Me
oThread = New Threading.Threa d(AddressOf oSession.Thread Main)
oThread.Apartme ntState = 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(C lient)
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(Cl ient)
End Sub
'---------------------------------------

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

'*************S ession.vb****** **************
Imports System.Threadin g
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.Register Client(Me, Thread.CurrentT hread()) = False Then
Exit Sub
End If

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

End Sub

Public Sub Terminate()
Terminated = True
End Sub

May 22 '06 #1
5 1645
Yes, simply "sleep" for a few miliseconds on each iteration.
Thread.Sleep(10 0)
"fniles" <fn****@pfmail. com> escribió en el mensaje
news:ez******** ******@TK2MSFTN GP05.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.Threadin g
Private Structure SessionInfo
Dim Session As SessionClass
Dim Thread As Threading.Threa d
End Structure
Private Client(0) As SessionInfo
Private LastClient As Integer
Private m_cnt As Long

Private Sub TestButton_Clic k(ByVal sender As Object, ByVal e As
System.EventArg s) Handles TestButton.Clic k
Dim oSession As SessionClass
Dim oThread As Threading.Threa d
Static nSession As Long

nSession = nSession + 1
oSession = New SessionClass
oSession.Server = Me
oThread = New Threading.Threa d(AddressOf oSession.Thread Main)
oThread.Apartme ntState = 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(C lient)
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(Cl ient)
End Sub
'---------------------------------------

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

'*************S ession.vb****** **************
Imports System.Threadin g
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.Register Client(Me, Thread.CurrentT hread()) = False Then
Exit Sub
End If

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

End Sub

Public Sub Terminate()
Terminated = True
End Sub

May 22 '06 #2
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.Threadin g
Private Structure SessionInfo
Dim Session As SessionClass
Dim Thread As Threading.Threa d
End Structure
Private Client(0) As SessionInfo
Private LastClient As Integer
Private m_cnt As Long

Private Sub TestButton_Clic k(ByVal sender As Object, ByVal e As
System.EventArg s) Handles TestButton.Clic k
Dim oSession As SessionClass
Dim oThread As Threading.Threa d
Static nSession As Long

nSession = nSession + 1
oSession = New SessionClass
oSession.Server = Me
oThread = New Threading.Threa d(AddressOf oSession.Thread Main)
oThread.Apartme ntState = 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(C lient)
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(Cl ient)
End Sub
'---------------------------------------

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

'*************S ession.vb****** **************
Imports System.Threadin g
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.Register Client(Me, Thread.CurrentT hread()) = False Then
Exit Sub
End If

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

End Sub

Public Sub Terminate()
Terminated = True
End Sub

May 22 '06 #3
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(10 0) '---->>> 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(Cl ient)
End Sub
'---------------------------------------

"Fred Hedges" <do******@spamm uch.com> wrote in message
news:e4******** ***********@new s.demon.co.uk.. .
Yes, simply "sleep" for a few miliseconds on each iteration.
Thread.Sleep(10 0)
"fniles" <fn****@pfmail. com> escribió en el mensaje
news:ez******** ******@TK2MSFTN GP05.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.Threadin g
Private Structure SessionInfo
Dim Session As SessionClass
Dim Thread As Threading.Threa d
End Structure
Private Client(0) As SessionInfo
Private LastClient As Integer
Private m_cnt As Long

Private Sub TestButton_Clic k(ByVal sender As Object, ByVal e As
System.EventArg s) Handles TestButton.Clic k
Dim oSession As SessionClass
Dim oThread As Threading.Threa d
Static nSession As Long

nSession = nSession + 1
oSession = New SessionClass
oSession.Server = Me
oThread = New Threading.Threa d(AddressOf oSession.Thread Main)
oThread.Apartme ntState = 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(C lient)
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(Cl ient)
End Sub
'---------------------------------------

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

'*************S ession.vb****** **************
Imports System.Threadin g
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.Register Client(Me, Thread.CurrentT hread()) = False Then
Exit Sub
End If

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

End Sub

Public Sub Terminate()
Terminated = True
End Sub


May 22 '06 #4
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.Threadin g
Private Structure SessionInfo
Dim Session As SessionClass
Dim Thread As Threading.Threa d
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.Threa d) As Boolean
Dim nIndex As Integer
Dim nMaxClients As Integer

RegisterClient = False
nMaxClients = Val(MaxClients. Text)
Monitor.Enter(C lient)
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(Cl ient)
Exit Function
End If
Next

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

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

UnregisterClien t = False
Monitor.Enter(C lient)
For nIndex = 0 To LastClient - 1
If Client(nIndex). Session Is Session Then
Client(nIndex). Session = Nothing
Client(nIndex). Thread = Nothing
UnregisterClien t = True
Exit For
End If
Next
Monitor.Exit(Cl ient)
End Function

Private Sub TestButton_Clic k(ByVal sender As Object, ByVal e As
System.EventArg s) Handles TestButton.Clic k
Dim oSession As SessionClass
Dim oThread As Threading.Threa d
Static nSession As Long

nSession = nSession + 1
oSession = New SessionClass
oSession.Server = Me
oThread = New Threading.Threa d(AddressOf oSession.Thread Main)
oThread.Apartme ntState = 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(C lient)
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(Cl ient)
End Sub
'---------------------------------------

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

'*************S ession.vb****** **************
Imports System.Threadin g
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.Register Client(Me, Thread.CurrentT hread()) = False Then
Exit Sub
End If

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

End Sub

Public Sub Terminate()
Terminated = True
End Sub

"Patrice" <sc****@chez.co m> wrote in message
news:O1******** ******@TK2MSFTN GP05.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.Threadin g
Private Structure SessionInfo
Dim Session As SessionClass
Dim Thread As Threading.Threa d
End Structure
Private Client(0) As SessionInfo
Private LastClient As Integer
Private m_cnt As Long

Private Sub TestButton_Clic k(ByVal sender As Object, ByVal e As
System.EventArg s) Handles TestButton.Clic k
Dim oSession As SessionClass
Dim oThread As Threading.Threa d
Static nSession As Long

nSession = nSession + 1
oSession = New SessionClass
oSession.Server = Me
oThread = New Threading.Threa d(AddressOf oSession.Thread Main)
oThread.Apartme ntState = 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(C lient)
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(Cl ient)
End Sub
'---------------------------------------

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

'*************S ession.vb****** **************
Imports System.Threadin g
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.Register Client(Me, Thread.CurrentT hread()) = False Then
Exit Sub
End If

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

End Sub

Public Sub Terminate()
Terminated = True
End Sub


May 22 '06 #5
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******** ******@TK2MSFTN GP03.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(10 0) '---->>> 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(Cl ient)
End Sub
'---------------------------------------

"Fred Hedges" <do******@spamm uch.com> wrote in message
news:e4******** ***********@new s.demon.co.uk.. .
Yes, simply "sleep" for a few miliseconds on each iteration.
Thread.Sleep(10 0)
"fniles" <fn****@pfmail. com> escribió en el mensaje
news:ez******** ******@TK2MSFTN GP05.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.Threadin g
Private Structure SessionInfo
Dim Session As SessionClass
Dim Thread As Threading.Threa d
End Structure
Private Client(0) As SessionInfo
Private LastClient As Integer
Private m_cnt As Long

Private Sub TestButton_Clic k(ByVal sender As Object, ByVal e As
System.EventArg s) Handles TestButton.Clic k
Dim oSession As SessionClass
Dim oThread As Threading.Threa d
Static nSession As Long

nSession = nSession + 1
oSession = New SessionClass
oSession.Server = Me
oThread = New Threading.Threa d(AddressOf oSession.Thread Main)
oThread.Apartme ntState = 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(C lient)
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(Cl ient)
End Sub
'---------------------------------------

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

'*************S ession.vb****** **************
Imports System.Threadin g
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.Register Client(Me, Thread.CurrentT hread()) = False Then
Exit Sub
End If

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

End Sub

Public Sub Terminate()
Terminated = True
End Sub



May 23 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
453
by: iam1708 via DotNetMonster.com | last post by:
I was looking at the docs for Thread and can't understand the different between "unnamed data slot "and "data slot".and docs in Thread.GetData() say "Threads use a local store memory mechanism to store thread-specific data." what is "thread-specific data"? Best regards wmt email:iam1708@hotmail.com
6
3204
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked list within a single object, which is then placed on a stack(This cuts down thread creation and deletions roughly by a factor of 4). I create up to 12 threads, which then process a single object off of the stack. I use a loop with a boolean...
37
8899
by: ales | last post by:
Hello, I have a problem with creation of new thread. The method .Start() of newly created thread delays current thread for 0 - 1 second. Cpu while delay occurs is about 5%. Any idea? Here is code used for measuring:
1
1964
by: batista | last post by:
Hello all, I have a third praty grid control...named C1grid. Im using it in one of my apps.. Now, I have bind this grid to a custom dataset class named "DataViewEx". The code of the class is below... Now what happens is that im updating this dataviewex class from a separate thread.....
2
1962
by: Don | last post by:
How to stop a process which is running in a separate thread!!! I've got a class which performs some lengthy process in a background (separate) thread. And this lengthy process raises events regularly to inform the main thread of the progress (which is then displayed to the user). Since the event is raised from another thread, i've used me.Invoke() to update the ui properly. However, my problem is in cancelling the process. I need to...
2
4608
by: knightowl | last post by:
Hi everybody , I am trying to implement a test director that will test a program. I also need to timeout if the program takes too long to output the answer. So i am using a thread to keep testing the program ( sequentially , I.e. 1 test at a time ) I am unsure how to find out if the thread is taking too long with timeout value ( in seconds ) then I want to kill that thread ? how do I do that ? here is my code int success; success =...
8
16578
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
I have an application with several BackgroundWorker threads. I hoped I'd be able to just type backgroundworker1.Name = "bw1"; but I don't see a name property. Any thoughts on how to name a backgroundworker thread? Thanks, Randy
9
2982
by: =?Utf-8?B?YmJn?= | last post by:
Hi all, I read somewhere "using kernel stuff in thread is not good.." if ManualResetEvent object is created in thread but not actually used, will it affect performance? Bob
7
1636
by: Curious | last post by:
On Jun 10, 3:32 am, <s...@dailycoding.comwrote: Thanks! I'll use thread pool.
0
8917
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.