Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim p As New Pinger p.Start("192.168.1.1254") End Sub End Class Class Pinger Private who_ As String Private pingSender_ As Ping Public Event ReturnValue(value As String) Private cancel_ As Boolean = False Sub New() pingSender_ = New Ping AddHandler pingSender_.PingCompleted, AddressOf pingSender__PingCompleted End Sub Public Sub Cancel() cancel_ = True pingSender_.SendAsyncCancel() End Sub Public Sub Start(who As String) who_ = who RaiseEvent ReturnValue("Starting") PingProccess() End Sub Private Sub PingProccess() If cancel_ Then Exit Sub Dim waiter_ As New AutoResetEvent(False) Thread.Sleep(1000) Dim data As String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" Dim buffer() As Byte = Encoding.ASCII.GetBytes(data) Dim timeout As Integer = 1000 Dim options As New PingOptions(64, True) pingSender_.SendAsync(who_, timeout, buffer, options, waiter_) If cancel_ Then Exit Sub Application.DoEvents() End Sub Private Sub pingSender__PingCompleted(sender As Object, e As PingCompletedEventArgs) If e.Cancelled Then RaiseEvent ReturnValue("Ping cancel") CType(e.UserState, AutoResetEvent).Set() End If If Not IsNothing(e.Error) Then RaiseEvent ReturnValue("Ping error : ") RaiseEvent ReturnValue(e.Error.ToString) CType(e.UserState, AutoResetEvent).Set() End If Dim reply As PingReply = e.Reply DisplayReply(reply) CType(e.UserState, AutoResetEvent).Set() PingProccess() End Sub Private Sub DisplayReply(reply As PingReply) If IsNothing(reply) Then ElseIf reply.Status = IPStatus.Success Then RaiseEvent ReturnValue("Reply from " & reply.Address.ToString & ": bytes= " & reply.Buffer.Length & " time = " & reply.RoundtripTime & "ms TTL=" & reply.Options.Ttl) End If End Sub End Class
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)