F
fniles
I am having problem with thread. I have a Session class with public string
variable (called Message) that I set from my Main program. In the session
class it checks for the value of Message while inside it's "read loop"
waiting for data from the client. In the main program, I check to see if
the Message member has been cleared before changing its value, that way you
know it has been written by the
session thread. The idea would be that if a thread hasn't written its data
to the client yet, move on to the next thread and then go back and check
again on those threads that we're ready in the previous pass.
But, by doing this, the CPU usage is 100%.
Is there any way to do this without driving CPU usage to 100% ?
Thanks a lot.
'****************Main program*************************
Imports System.Threading
Private Structure SessionInfo
Dim Session As SessionClass
Dim Thread As Threading.Thread
End Structure
Private Client(0) As SessionInfo
Private LastClient As Integer
Private m_cnt As Long
Private Sub TestButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TestButton.Click
Dim oSession As SessionClass
Dim oThread As Threading.Thread
Static nSession As Long
nSession = nSession + 1
oSession = New SessionClass
oSession.Server = Me
oThread = New Threading.Thread(AddressOf oSession.ThreadMain)
oThread.ApartmentState = ApartmentState.STA
oThread.Name = "Session" & CLng(nSession)
oThread.Start()
Timer1.Enabled = True
End Sub
Sub Test()
Dim arr() As Long
Dim lMax As Long
Dim bLoop As Boolean
Dim x As Long
Dim nIndex As Integer
Monitor.Enter(Client)
For nIndex = 0 To LastClient - 1
If Not Client(nIndex).Session Is Nothing Then
If Client(nIndex).Session.Message = "" Then
Client(nIndex).Session.Message = m_cnt
Else
lMax = lMax + 1
ReDim Preserve arr(lMax)
arr(lMax) = nIndex
End If
End If
Next
If lMax > 0 Then bLoop = True Else bLoop = False
'--------------------------------------->>>>>the following takes up to 100%
CPU<<<<<<--------------------------------------------------------
Do While bLoop
bLoop = False
For x = 1 To lMax
nIndex = arr(x)
If nIndex > -1 Then
If Not Client(nIndex).Session Is Nothing Then
If Client(nIndex).Session.Message = "" Then
Client(nIndex).Session.Message = m_cnt
arr(x) = -1
Else
bLoop = True
End If
End If
End If
Next
Loop
Monitor.Exit(Client)
End Sub
'---------------------------------------
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
m_cnt = m_cnt + 1
Test()
End Sub
'*************Session.vb********************
Imports System.Threading
Public Class SessionClass
Public Server As ServerForm
Public Message As String
Private Terminated As Boolean
Public Sub ThreadMain()
Dim nThreadId As Integer
Dim strThreadName As String
Dim strBuffer As String
Dim nResult As Integer
Dim sw As StreamWriter
If Server.RegisterClient(Me, Thread.CurrentThread()) = False Then
Exit Sub
End If
Do While Not Terminated
If Len(Me.Message) > 0 Then
Me.Message = ""
End If
Loop
Server.UnregisterClient(Me)
Exit Sub
End Sub
Public Sub Terminate()
Terminated = True
End Sub
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