G
Guest
Hi, I have a class that use a TCPClient and do BeginRead to receive some data. When I receive the data I want to execute a method in the class thread not the BeginRead workerthread. I tried to use a delegate, but I don't seem to get it right. Anybody can help me with this. Thanks
Here is some cod
Public Class AccessContro
Const READ_BUFFER_SIZE As Integer = 25
Const PORT_NUM As Integer = 102
Private Client As TcpClien
Private EndPoint As Net.IPEndPoin
Private IP As Net.IPAddres
Private readBuffer(READ_BUFFER_SIZE) As Byt
Private Delegate Sub ShowNotificationWindow(ByVal Info As String
Private Del As ShowNotificationWindo
Sub New(ByVal Language As String, ByVal User As String
Del = New ShowNotificationWindow(AddressOf ShowNotification
Tr
IP = System.Net.IPAddress.Parse("192.168.250.100"
EndPoint = New Net.IPEndPoint(IP, PORT_NUM
Client = New TcpClien
Client.Connect(EndPoint
Client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing
SendData("CONNECT|" & CurrentUser
If Not IsServiceLocal The
AppTaskIcon.Icon = icoRunnin
End I
Catch Ex As Exceptio
MsgBox(Ex.Message
End Tr
End Su
Private Sub DoRead(ByVal ar As IAsyncResult
Dim BytesRead As Intege
Dim strMessage As Strin
Tr
' Finish asynchronous read into readBuffer and return number of bytes read
BytesRead = Client.GetStream.EndRead(ar
If BytesRead < 1 The
' If no bytes were read server has close. Disable input window
Exit Su
End I
' Convert the byte array the message was saved into, minus two for th
' Chr(13) and Chr(10
strMessage = System.Text.Encoding.ASCII.GetString(readBuffer, 0, BytesRead - 2
ProcessCommands(strMessage
' Start a new asynchronous read into readBuffer
Client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing
Catch ex As Exceptio
MsgBox(ex.Message
End Tr
End Su
Private Sub ProcessCommands(ByVal strMessage As String
Dim dataArray() As Strin
dataArray = strMessage.Split(Chr(124)
Select Case dataArray(0
Case "JOIN
AppTaskIcon.Text = "CimSport - Centre d'information (Connecté)
Case "REFUSE
AppTaskIcon.Text = "CimSport - Centre d'information (Déconnecté)
Case "BROAD
Del.Invoke(dataArray(1)
End Selec
End Su
Private Sub ShowNotification(ByVal Info As String
Dim f As New frmNotificatio
f.PictureBox1.Image = Image.FromFile("C:\Documents and Settings\mhardy\My Documents\My Pictures\Bart1.jpg"
f.LinkLabel1.Text = Inf
AddHandler f.TextClicked, AddressOf NotificationWindow_Notification_Clicke
'ActiveNotification +=
ShowWindow(f.Handle, SW_SHOWNOACTIVE
f.Left = Screen.PrimaryScreen.WorkingArea.Width - f.Widt
'If ActiveNotification > 1 The
' f.Top = Screen.PrimaryScreen.WorkingArea.Height - (f.Height * (ActiveNotification - 1)
'Els
f.Top = Screen.PrimaryScreen.WorkingArea.Height - f.Heigh
'End I
End Su
end clas
Here is some cod
Public Class AccessContro
Const READ_BUFFER_SIZE As Integer = 25
Const PORT_NUM As Integer = 102
Private Client As TcpClien
Private EndPoint As Net.IPEndPoin
Private IP As Net.IPAddres
Private readBuffer(READ_BUFFER_SIZE) As Byt
Private Delegate Sub ShowNotificationWindow(ByVal Info As String
Private Del As ShowNotificationWindo
Sub New(ByVal Language As String, ByVal User As String
Del = New ShowNotificationWindow(AddressOf ShowNotification
Tr
IP = System.Net.IPAddress.Parse("192.168.250.100"
EndPoint = New Net.IPEndPoint(IP, PORT_NUM
Client = New TcpClien
Client.Connect(EndPoint
Client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing
SendData("CONNECT|" & CurrentUser
If Not IsServiceLocal The
AppTaskIcon.Icon = icoRunnin
End I
Catch Ex As Exceptio
MsgBox(Ex.Message
End Tr
End Su
Private Sub DoRead(ByVal ar As IAsyncResult
Dim BytesRead As Intege
Dim strMessage As Strin
Tr
' Finish asynchronous read into readBuffer and return number of bytes read
BytesRead = Client.GetStream.EndRead(ar
If BytesRead < 1 The
' If no bytes were read server has close. Disable input window
Exit Su
End I
' Convert the byte array the message was saved into, minus two for th
' Chr(13) and Chr(10
strMessage = System.Text.Encoding.ASCII.GetString(readBuffer, 0, BytesRead - 2
ProcessCommands(strMessage
' Start a new asynchronous read into readBuffer
Client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing
Catch ex As Exceptio
MsgBox(ex.Message
End Tr
End Su
Private Sub ProcessCommands(ByVal strMessage As String
Dim dataArray() As Strin
dataArray = strMessage.Split(Chr(124)
Select Case dataArray(0
Case "JOIN
AppTaskIcon.Text = "CimSport - Centre d'information (Connecté)
Case "REFUSE
AppTaskIcon.Text = "CimSport - Centre d'information (Déconnecté)
Case "BROAD
Del.Invoke(dataArray(1)
End Selec
End Su
Private Sub ShowNotification(ByVal Info As String
Dim f As New frmNotificatio
f.PictureBox1.Image = Image.FromFile("C:\Documents and Settings\mhardy\My Documents\My Pictures\Bart1.jpg"
f.LinkLabel1.Text = Inf
AddHandler f.TextClicked, AddressOf NotificationWindow_Notification_Clicke
'ActiveNotification +=
ShowWindow(f.Handle, SW_SHOWNOACTIVE
f.Left = Screen.PrimaryScreen.WorkingArea.Width - f.Widt
'If ActiveNotification > 1 The
' f.Top = Screen.PrimaryScreen.WorkingArea.Height - (f.Height * (ActiveNotification - 1)
'Els
f.Top = Screen.PrimaryScreen.WorkingArea.Height - f.Heigh
'End I
End Su
end clas