473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.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
5 1617
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
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...
6
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...
37
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...
1
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...
2
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...
2
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...
8
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...
9
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
by: Curious | last post by:
On Jun 10, 3:32 am, <s...@dailycoding.comwrote: Thanks! I'll use thread pool.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.