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?
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
Timercontrol 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: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.
A new
Timercontrol 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:Switch to code view and add a class-level variable of type
Integerto 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
Intervalof theTimeris 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.Add an event handler method for the
Timer.Tickevent. This is where the code that I just talked about above will go.So, your final code might look something like this: