Visual Basic Pinger

时间:2015-05-05 01:29:56

标签: vb.net visual-studio network-programming ping

我已经掌握了这个并且运行良好,但是无法想到程序告诉我ping响应是否失败的方法。我希望在出现错误时显示在列表框中。 任何帮助将不胜感激。

Option Explicit On
Option Infer Off

Imports System.Net.NetworkInformation
Imports System.ComponentModel

Public Class Form1

    Private WithEvents bwPing As New BackgroundWorker
    Private pingTarget As String
    Private pingsize As Integer
    Private numOfpings As Byte
    Dim timeout As Integer

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        ' start pinger
        bwPing.WorkerReportsProgress = True
        bwPing.WorkerSupportsCancellation = True
        pingTarget = TextBox2.Text
        timeout = ComboBox4.Text

        If Not bwPing.IsBusy Then bwPing.RunWorkerAsync()

        If ComboBox3.Text & ComboBox1.Text = "" Then
            bwPing.CancelAsync()
            ListBox1.Items.Add("*****!!!!!***** INVALID ENTRY *****!!!!!*****")
            MsgBox(" By failing to prepare, you are preparing to fail. ", MsgBoxStyle.Exclamation)
        ElseIf ComboBox3.Text = "" Then
            bwPing.CancelAsync()
            ListBox1.Items.Add("*****!!!!!***** INVALID ENTRY *****!!!!!*****")
            MsgBox(" How many troops are you sending in? ", MsgBoxStyle.Exclamation)
        ElseIf ComboBox1.Text = "" Then
            bwPing.CancelAsync()
            ListBox1.Items.Add("*****!!!!!***** INVALID ENTRY *****!!!!!*****")
            MsgBox(" How strong are your soldiers? ", MsgBoxStyle.Exclamation)
        Else : numOfpings = CInt(ComboBox3.Text)
            pingsize = CInt(ComboBox1.Text)
        End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ' cancel pinger
        bwPing.CancelAsync()
    End Sub

    Private Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
        ' clear
        ListBox1.Items.Clear()
        ComboBox1.Text = ""
        ComboBox3.Text = ""
        ProgressBar1.Value = 0
    End Sub

    Private Sub bwPing_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles bwPing.DoWork
        ' ping worker
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        Dim packet(pingsize) As Byte

        For i As Integer = 0 To numOfpings - 1
            bwPing.ReportProgress(i + 1)
            Dim ping As New Ping
            Dim reply As PingReply = ping.Send(pingTarget, timeout, packet)
            If ComboBox3.Text & ComboBox1.Text = "" Then
                bwPing.CancelAsync()
            Else
                ListBox1.Items.Add("You hit " & pingTarget & " in " & reply.RoundtripTime.ToString() & " ms with " & pingsize & " bytes.")
                System.Threading.Thread.Sleep(500)
            End If

            If worker.CancellationPending Then Exit For
        Next

    End Sub

        Private Sub bwPing_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles bwPing.ProgressChanged
        ' update results
        Me.ProgressBar1.Value = e.ProgressPercentage
        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = numOfpings
    End Sub

    Private Sub bwPing_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles bwPing.RunWorkerCompleted
        ' finished
        Me.ListBox1.Items.Add("*!* The battle is over, but not the war *!*")
        Me.ListBox1.Items.Add("::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
        bwPing.CancelAsync()

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False
    End Sub


End Class

1 个答案:

答案 0 :(得分:0)

通常,请记住您可以检查对象是否有任何事件。通常,当一个对象有一个已完成的事件时,事件args中的某个地方,将在那些参数中为您提供相关的事物,例如完成状态。话虽如此,处理ping完成事件。

附注*将CheckForIllegalCrossThreadCalls设置为false不是一个好主意,而是使用委托。

示例(用于ping)

Option Strict On
Option Explicit On
Option Infer Off
Imports System.Net
Imports System.Net.NetworkInformation
Public Class Form1
    Private a As New Ping
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AddHandler a.PingCompleted, AddressOf a_PingCompleted
    End Sub
    Private Sub a_PingCompleted(sender As Object, e As PingCompletedEventArgs)
        If e.Reply.Status.ToString = "Success" Then
            MsgBox("Round Trip Time: " & e.Reply.RoundtripTime.ToString & "ms")
        Else
            MsgBox(e.Reply.Status.ToString)
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Google's ip address
        a.SendAsync(New IPAddress({74, 125, 224, 105}), 100)
    End Sub
End Class