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

LVL 8
matrix_aashAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi matrix_aash;

The reason why you were having problems is that you had the code to update the TextBox within the new thread. Once the txtPing.InvokeRequired() command was executed in the new thread it continued executing in the GUI thread after the creation of the delegate because when the delegate took over it never finishes. I rearranged your code so that it will cork correctly.

Fernando
Imports System.Net.NetworkInformation
Imports System.Threading
 
Public Class Form1
 
    ' You should keep a reference to a thread
    Dim pingThread As Thread
 
    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
 
        ' Create the thread
        pingThread = New Thread(AddressOf Me.PingNetwork)
        ' Make it of type background
        pingThread.IsBackground = True
        ' Start the thread
        pingThread.Start()
 
    End Sub
 
 
    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)
                ' Format message to send to txtPing
                Dim message As String = String.Format("{0} - {1} ({3}})", _
                    pingreply.Address.ToString, pingreply.Status.ToString, _
                    pingreply.RoundtripTime)
                ' Update the txtPing TextBox
                UpdateGUI(message)
            Catch err As Exception
                ' Update the txtPing TextBox
                UpdateGUI(String.Format("10.100.25.{0} - Dead", s))
            End Try
 
            s += 1
 
        Loop
 
    End Sub
 
    ' Function pointer to method to update GUI
    Private Delegate Sub UpdateGUIDelegate(ByVal message As String)
 
    ' Method to update the GUI/txtPing
    Private Sub UpdateGUI(ByVal message As String)
 
        ' Check to see who is calling this method. If other then GUI thread
        ' create delegate to make the call.
        If txtPing.InvokeRequired() Then
            ' Get the GUI thread to update the txtPing control
            Dim d As New UpdateGUIDelegate(AddressOf UpdateGUI)
            Me.Invoke(d, message)
        Else
            ' Update the txtPing text box.
            txtPing.AppendText(message & Environment.NewLine)
        End If
 
    End Sub
 
 
End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gregoryyoungCommented:
beyond Fernando's solution I would recommend moving from the blocking methods in a seperate thread to non-blocking methods such as PingAsync http://msdn2.microsoft.com/en-us/library/0e6kc029.aspx then taking the callback

Cheers,

Greg
0
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.