Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 525
  • Last Modified:

Events and Async callbacks

Hi

I need help how to raise an event in a the method below, I'll get an exception when raising the event now. It sems to have something to do with the Async callback in the tcpclient method.
Private _tcpClient As TcpClient  
 Public Event NewSocketData(ByVal data As String)
 
        Private Sub DoRead(ByVal ar As IAsyncResult)
            Dim bytesRead As Integer
            Dim message As String
 
            Try
                '// Finish asynchronous read into readBuffer and return number of bytes read.
                bytesRead = Me._tcpClient.GetStream.EndRead(ar)
                If bytesRead < 1 Then
                    ' If no bytes were read server has closed. 
                    Me._socketdata.Add("Connection was terminated")
                    RaiseEvent NewSocketData("Connection was terminated")
                    Exit Sub
                End If
 
                '// Convert the byte array the message was saved into, minus two for the
                '// Chr(13) and Chr(10)
                message = Encoding.ASCII.GetString(Me._readBuffer, 0, bytesRead - 2)
 
                ProcessCommands(message)
            Catch ex As Exception
                MessageBox.Show(ex.StackTrace.ToString)
            End Try
 
'Need help here.. this does not work
            RaiseEvent NewSocketData("test")
 
            '// Start a new asynchronous read into readBuffer.
            Me._tcpClient.GetStream.BeginRead(Me._readBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)
 
        End Sub

Open in new window

0
AWestEng
Asked:
AWestEng
  • 8
  • 3
2 Solutions
 
sunithnairCommented:
What is the exception message you are getting?
0
 
AWestEngAuthor Commented:
Parameter count mismatch
0
 
sunithnairCommented:
Have you subscribed for the event? Are you getting the error at line 18?
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
AWestEngAuthor Commented:
I get the exception on  RaiseEvent
This is waht I do,
Public class SocketClient
  Public Event NewSocketData(ByVal data As String)

   Private Sub DoRead(ByVal ar As IAsyncResult)
          -------------------
    End Sub
 
    -------------

En Class
 
Public Class frmMAin
Private WithEvents _foo As New SocketClient
 
Private Sub NewTCPSocketData(ByVal data As String) Handles _foo .NewSocketData
     MessageBox.Show(data)
End Sub
End Class
 
0
 
sunithnairCommented:
I dont see anything any wrong and I dont know why the parameter count mismatcherror occurs in this situation. Is there any other message shown in the InnerException? Try adding this in your frmMain class
Public Class frmMAin
Private WithEvents _foo As New SocketClient
 
 
Private Sub NewTCPSocketData(ByVal data As String) Handles _foo .NewSocketData
     MessageBox.Show(data)
     My.Application.DoEvents()
End Sub
 
End Class

Open in new window

0
 
AWestEngAuthor Commented:
I found that the problem came from the UpdateListview() , when is executeing me.invoke the exception was thrown.
 
 system.Reflection.TargerparameterCountExceptio> Parametrer count msmatch at system.windows.froms.control.MarshaledInvoke(controller caller, Delegate methods,Onbject[] args, boolean synchronous) at system.windows.forms.control.Invoke(Delagate method, Objec[] args))
 


Private Sub NewTCPSocketData(ByVal data As String) Handles _foo .NewSocketData
     UpdateListview(data)
End Sub
 
   Private Delegate Sub UpdateListviewDelegate(ByVal message As String, ByVal type As ImageType, ByVal showdate As Boolean)
 
 Public Sub UpdateListview(ByVal message As String) Implements IConsole.UpdateListview
        If Me.InvokeRequired Then
            Me.Invoke(New UpdateListviewDelegate(AddressOf UpdateListview), New Object(), message, type, showdate)
        Else
 
        End If
    End Sub

Open in new window

0
 
AWestEngAuthor Commented:
copy paste error>
the delagate should look like this
Private Delegate Sub UpdateListviewDelegate(ByVal message As String)
 
 
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Instead of:

    Me.Invoke(New UpdateListviewDelegate(AddressOf UpdateListview), New Object(), message, type, showdate)

Try:

    Me.Invoke(New UpdateListviewDelegate(AddressOf UpdateListview), New Object() {message, type, showdate})

0
 
AWestEngAuthor Commented:
What is the difference?
 
0
 
AWestEngAuthor Commented:
I can see the {}, has the invoke a overload constructor that can read this in a wrong way if I dont use the {}?
0
 
AWestEngAuthor Commented:
Idle_Mind... YOUR THE BEST!!! Thx.... hehe...
0
 
AWestEngAuthor Commented:
sunithnair, good job you did not have all info.
Idle_Mind: Great job as you always do,,
Thx Guys!!!!!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 8
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now