How can I periodically ping a server to determine

2019-08-18 08:25发布

问题:

I hope I am asking this correctly...

What I am looking to do is ping a server every 30 minutes to an hour or so and to send an email if the server is not pingable. At the moment, all I have is the following code:

Private Sub Button1_Click(ByVal sender As System.Object,
                          ByVal e As System.EventArgs) Handles Button1.Click
    If My.Computer.Network.Ping("209.85.143.99") Then
        MsgBox("Server pinged successfully.")
    Else
        MsgBox("Ping request timed out.")
    End If
End Sub

I am creating the project in Visual Studio 2010. All it does now is check if it can ping a server. I would like it to run this code every 30min. Can anyone tell me how I can do that?

回答1:

The code that you already you have is a good start, but (as you probably know) it will only be executed when the user clicks the button. If you need to ping the server periodically every 30 minutes, you will need to start a timer and run your code each time the timer has elapsed.

A Timer control is already provided for you by the .NET Framework, and it's quite simple to take advantage of its functionality. Here's a quick step-by-step:

  1. In Design Mode (where you can edit your form from inside of Visual Studio), scroll down to the "Components" tab in the Toolbox and drag the "Timer" control to your form.

  2. A new Timer control will appear in a gray box at the bottom of your screen. Use the Properties Window to give it a name and set its properties:

    • Name: pingTimer
    • Enabled: True
    • Interval: 60000

  3. Switch to code view and add a class-level variable of type Integer to your form. This variable will be used to keep track of how many seconds have elapsed so far.

    The reason you have to do this is the Interval of the Timer is measured in milliseconds, which makes its maximum value far less than 30 minutes. By setting the interval to "60000", the timer will "tick" every 1 minute, and on each tick, you will increment the variable keeping track of the total number of timer ticks. When the value of that variable reaches 30 (indicating 30 minutes has elapsed), you will reset it to 0 and run your ping code.

  4. Add an event handler method for the Timer.Tick event. This is where the code that I just talked about above will go.

So, your final code might look something like this:

' Class-level variable to keep track of the number of "ticks" so far
Private elapsedTime As Integer = 0

Private Sub pingTimer_Tick(ByVal sender As System.Object,
                           ByVal e As System.EventArgs) Handles pingTimer.Tick
    ' Check the time that has elapsed so far
    If elapsedTime >= 30 Then
        ' It has been 30 minutes, so reset the elapsed time count
        elapsedTime = 0

        ' And do the ping
        If My.Computer.Network.Ping("209.85.143.99") Then
            MsgBox("Server pinged successfully.")
        Else
            MsgBox("Ping request timed out.")
        End If
    Else
        ' It has not yet been 30 minutes, so increment the elapsed time count
        elapsedTime += 1
    End If
End Sub


回答2:

Use the Timer control, more info at MSDN:

http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx



回答3:

set the interval to 60*1,000*number_of_minutes

unsigned 32 bit ints can hold values up to 4,294,967,295, so even a signed value can hold half of that - so thats still like 35,000 minutes before the timer overflows. (i dont know off the top of my head if the timers uses signed or unsiged - unsiged would make sense but who knows)

you do not need to increment a separate variable as suggested by Cody Gray above.

I have successfully used a timer that counted to 23 hours, and had no issues. That's 82,800,000ms, by the way, which is nowhere near overflowing a 32 bit int, signed or not.

I've never sent an email from VB.net before, but i suggest you look in System.Net.Mail - i googled it and this popped up: http://osix.net/modules/article/?id=377