I have a server applicatoin that has a multitude of labels related to the clients connected to it. When I have a number near 50 clients and i recieve a updatated status from each of the clients at once the labels are updated to the current status. (basically a label color is changed depending on status), this operation causes the mainform to freeze until it can process all of the messages recieved.
I have tried everything possible to make the form update asyncronously without this hang but cannot get it to work. Since each client thread runs the same update function i placed in a multitude of invoke code to make it update but there has been no change.
So my next idea was to make the from unclickable until its ready but am unsure how to do this? anyone got any ideas?
example of process:
[CODE=vbnet]server:
sendtoclients(" GETSTATUS")
client:
client("RETURNS TATUS|GREEN")
server:
case "RETURNSTAT US"
If Not statusdict.Cont ainsKey(sender. WS) Then
statusdict.Add( sender.WS, dataArray(1))
Else
statusdict.Item (sender.WS) = dataArray(1)
End If
''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' '''''''''''''
UpdateUI()
TargetHandler.B eginInvoke(Call backHandler, Nothing)
'''''old method call 'readstatusdict oinary()
end select
sub readstatusentry ()
Dim entry As DictionaryEntry
Dim comp As String
Dim status As String
Dim ILoop As Integer = 0
Dim ALoop As Integer = 0
Dim green As Integer = 0
Dim red As Integer = 0
'nulls(display)
loggedInLabel.T ext = "0"
LockedLabel.Tex t = "0"
'HERE is where it changes label color
Try
For Each entry In New System.Collecti ons.Hashtable(s tatusdict)
ALoop = 0
comp = entry.Key.ToStr ing
status = entry.Value.ToS tring
For ILoop = 0 To 69
If comp = LabelArray(ALoo p).Text Then
Select Case status.ToLower
Case "red"
LabelArray(ALoo p).BackColor = Color.Red
red = red + 1
green = green - 1
lockedlabel.Tex t = red.ToString
Case "green"
LabelArray(ALoo p).BackColor = Color.Green
green = green + 1
red = red - 1
loggedInLabel.T ext = green.ToString
End Select
PNRLabel.Text = gray.ToString
End If
ALoop = ALoop + 1
Next
Next
Catch ex As Exception
MsgBox(ex.ToStr ing)
End Try
end sub
'*** create delegate object to execute method asynchronously
Private TargetHandler As readstatusentry Handler _
= AddressOf readstatusentry
'*** create delegate object to service callback from CLR
Private CallbackHandler As AsyncCallback _
= AddressOf MyCallbackMetho d
'*** callback method runs on worker thread and not the UI thread
Sub MyCallbackMetho d(ByVal ar As IAsyncResult)
Try
UpdateUI()
Catch ex As Exception
MsgBox("Error: " & ex.Message)
UpdateUI()
End Try
End Sub
'*** can be called from any method on form to update UI
Sub UpdateUI()
'*** check to see if thread switch is required
If Me.InvokeRequir ed Then
Dim handler As New UpdateUIHandler (AddressOf readstatusentry )
Me.BeginInvoke( handler)
Else
readstatusentry ()
End If
End Sub
'*** delegate used to switch control over to primary UI thread
Delegate Sub UpdateUIHandler ()
Delegate Sub readstatusentry Handler()[/CODE]
I have tried everything possible to make the form update asyncronously without this hang but cannot get it to work. Since each client thread runs the same update function i placed in a multitude of invoke code to make it update but there has been no change.
So my next idea was to make the from unclickable until its ready but am unsure how to do this? anyone got any ideas?
example of process:
[CODE=vbnet]server:
sendtoclients(" GETSTATUS")
client:
client("RETURNS TATUS|GREEN")
server:
case "RETURNSTAT US"
If Not statusdict.Cont ainsKey(sender. WS) Then
statusdict.Add( sender.WS, dataArray(1))
Else
statusdict.Item (sender.WS) = dataArray(1)
End If
''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' '''''''''''''
UpdateUI()
TargetHandler.B eginInvoke(Call backHandler, Nothing)
'''''old method call 'readstatusdict oinary()
end select
sub readstatusentry ()
Dim entry As DictionaryEntry
Dim comp As String
Dim status As String
Dim ILoop As Integer = 0
Dim ALoop As Integer = 0
Dim green As Integer = 0
Dim red As Integer = 0
'nulls(display)
loggedInLabel.T ext = "0"
LockedLabel.Tex t = "0"
'HERE is where it changes label color
Try
For Each entry In New System.Collecti ons.Hashtable(s tatusdict)
ALoop = 0
comp = entry.Key.ToStr ing
status = entry.Value.ToS tring
For ILoop = 0 To 69
If comp = LabelArray(ALoo p).Text Then
Select Case status.ToLower
Case "red"
LabelArray(ALoo p).BackColor = Color.Red
red = red + 1
green = green - 1
lockedlabel.Tex t = red.ToString
Case "green"
LabelArray(ALoo p).BackColor = Color.Green
green = green + 1
red = red - 1
loggedInLabel.T ext = green.ToString
End Select
PNRLabel.Text = gray.ToString
End If
ALoop = ALoop + 1
Next
Next
Catch ex As Exception
MsgBox(ex.ToStr ing)
End Try
end sub
'*** create delegate object to execute method asynchronously
Private TargetHandler As readstatusentry Handler _
= AddressOf readstatusentry
'*** create delegate object to service callback from CLR
Private CallbackHandler As AsyncCallback _
= AddressOf MyCallbackMetho d
'*** callback method runs on worker thread and not the UI thread
Sub MyCallbackMetho d(ByVal ar As IAsyncResult)
Try
UpdateUI()
Catch ex As Exception
MsgBox("Error: " & ex.Message)
UpdateUI()
End Try
End Sub
'*** can be called from any method on form to update UI
Sub UpdateUI()
'*** check to see if thread switch is required
If Me.InvokeRequir ed Then
Dim handler As New UpdateUIHandler (AddressOf readstatusentry )
Me.BeginInvoke( handler)
Else
readstatusentry ()
End If
End Sub
'*** delegate used to switch control over to primary UI thread
Delegate Sub UpdateUIHandler ()
Delegate Sub readstatusentry Handler()[/CODE]
Comment