Results 1 to 9 of 9

Thread: [RESOLVED] Threading / Delegates

Threaded View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Resolved [RESOLVED] Threading / Delegates

    I'm getting myself really confused.

    The idea is to Ping all the addresses in a given Class C sub-net and analyse the responses. Doing this synchronously takes a long time so I thought I'd use the Threading capability of VB2008 to start a new thread to Ping each Address, using Delegates to start the thread and process the results.
    Code:
    Public Class Form1
        Private Delegate Sub DoResult(ByVal pr As PingReply)
        Private Delegate Sub StartPing(ByVal IPCNumber As Integer)
        Private IPSubNet As String
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            IPSubNet = "192.168.1"
            For IPCounter As Integer = 0 To 254
                Dim myPing As StartPing = AddressOf DoPing
                Dim PingParam As Integer = IPCounter
                myPing.Invoke(PingParam)
            Next
        End Sub
    
        Public Sub ShowResult(ByVal pr As PingReply)
            Debug.Print("Address: " + pr.Address.ToString + " Status: " + pr.Status.ToString)
        End Sub
    
        Public Sub DoPing(ByVal ipn As Integer)
            Dim IPToPingC As String = IPSubNet + ipn.ToString
            Dim PingOp As New Ping
            Dim Pingresult As PingReply = PingOp.Send(IPToPingC)
            Dim ADResult As New DoResult(AddressOf ShowResult)
            ADResult.Invoke(Pingresult)
        End Sub
    End Class
    The above is failing with a Null Exception at ' Debug.Print("Address: " + pr.Address.ToString + " Status: " + pr.Status.ToString)'
    Having used the Debugger to look at PingResult in DoPing it's Null, I don't understand why? At the point of failure, IPToPingC has a vaule of "192.168.1.10" and nothing is being output to the Debug Window (so what happened to "192.168.1.0" through to "192.168.1.9" is anyones guess) - and I've only just started using VB2008.

    Never having used Threading I think I'm a little (lot) out of my depth, any assistance would be gratefully received.

    EDIT: Now realised that I should perhaps be using DynamicInvoke for starting DoPing and using Object type as the parameter rather than Invoke but it then fails with :
    A first chance exception of type 'System.MissingMemberException' occurred in Microsoft.VisualBasic.dll
    A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

    EDIT2: Just realised what the error message means - I have left the final period off the IPSubNet assignment. It now doesn't fail, but I'm getting wierd results
    Code:
    Address: 192.168.1.104 Status: DestinationHostUnreachable
    Address: 192.168.1.1 Status: Success
    Address: 192.168.1.104 Status: DestinationHostUnreachable
    Address: 192.168.1.104 Status: DestinationHostUnreachable
    Address: 192.168.1.104 Status: DestinationHostUnreachable
    Address: 192.168.1.104 Status: DestinationHostUnreachable
    192.168.1.1 is the gateway and 192.168.1.104 is the address of the PC running this code. Obviously there's some sort of re-synchronisation problem or the code is just not doing what I think it should.
    Current stae of the code:
    Code:
    Imports System.Net.NetworkInformation
    Imports System.Threading
    
    Public Class Form1
        Private Delegate Sub DoResult(ByVal pr As PingReply)
        Private Delegate Sub StartPing(ByVal IPAdd As Integer)
        Private IPSubNet As String
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            IPSubNet = "192.168.1."
            For IPCounter As Integer = 0 To 254
                Dim myPing As StartPing = AddressOf DoPing
                Dim PingParam As Integer = IPCounter
                myPing.DynamicInvoke(PingParam)
            Next
        End Sub
    
        Public Sub ShowResult(ByVal pr As PingReply)
            Debug.Print("Address: " + pr.Address.ToString + " Status: " + pr.Status.ToString)
        End Sub
    
        Public Sub DoPing(ByVal ipn As Integer)
            Dim IPToPingC As String = IPSubNet + ipn.ToString
            Dim PingOp As New Ping
            Dim Pingresult As PingReply = PingOp.Send(IPToPingC)
            Dim ADResult As New DoResult(AddressOf ShowResult)
            ADResult.Invoke(Pingresult)
        End Sub
    End Class
    Last edited by Doogle; May 27th, 2012 at 12:31 AM. Reason: Added EDIT 2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width