Link to home
Start Free TrialLog in
Avatar of matrix_aash
matrix_aashFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Delegate Threading

I am making a script and am trying to get my head around delegates and threading. I can't see to work it out. Could anyone help me try explain what it all does as i'm a bit confused.

I am trying it with the below code. The problems with it are:

a) It dosen't seem to be pinging on a separate thread as the application is freezing when it is pinging an IP
b) Once it has reached 225 it then loops again and doing Thread.CurrentThread.Abort() shuts down my application, not the pinging thread

Any help would be appreciated!
Imports System.Threading
Imports System.Net.NetworkInformation
 
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim pingThread As New Thread(AddressOf Me.PingNetwork)
        pingThread.IsBackground = True
        pingThread.Start()
    End Sub
 
    Private Delegate Sub PingNetworkDelegate()
 
    Private Sub PingNetwork()
        Dim s As Integer = 1
        Do Until s = 256
            Try
                Dim ping As Ping = New Ping()
                Dim pingreply As PingReply = ping.Send("10.100.25." & s, 500)
 
                If txtPing.InvokeRequired() Then
                    Dim d As New PingNetworkDelegate(AddressOf PingNetwork)
                    Me.Invoke(d, New Object() {})
                Else
                    txtPing.Text &= pingreply.Address.ToString & " - " & pingreply.Status.ToString & " (" & pingreply.RoundtripTime & ")" & vbCrLf
                End If
            Catch err As Exception
                txtPing.Text &= "10.100.25." & s & " - Dead" & "" & vbCrLf
            End Try
            s += 1
        Loop
    End Sub
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forced accept.

Computer101
EE Admin