如何定期 ping 服务器以确定它是否可从 VB.NET 应用程序使用? [英] How can I periodically ping a server to determine if it is available from a VB.NET app?

查看:28
本文介绍了如何定期 ping 服务器以确定它是否可从 VB.NET 应用程序使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我问得对...

我想要做的是每 30 分钟到一个小时左右 ping 一次服务器,如果服务器无法 ping 通,则发送电子邮件.目前,我只有以下代码:

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

我正在 Visual Studio 2010 中创建项目.它现在所做的只是检查它是否可以 ping 服务器.我希望它每 30 分钟运行一次此代码.谁能告诉我怎么做?

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?

推荐答案

您已有的代码是一个好的开始,但是(您可能知道)它只会在用户单击按钮时执行.如果您需要每 30 分钟定期 ping 服务器,您将需要启动一个计时器并在每次计时器结束时运行您的代码.

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.

Timer 控件 已经由 .NET Framework 为您提供,利用其功能非常简单.下面是一个快速的分步说明:

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. 在设计模式下(您可以从 Visual Studio 内部编辑您的表单),向下滚动到工具箱中的组件"选项卡,然后将计时器"控件拖到您的表单中.

  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.

一个新的 Timer 控件将出现在屏幕底部的灰色框中.使用属性窗口为其命名并设置其属性:

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:

  • 名称:pingTimer
  • 启用:真
  • 间隔:60000

切换到代码视图并将 Integer 类型的类级变量添加到您的表单中.此变量将用于跟踪到目前为止已经过去了多少秒.

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.

您必须这样做的原因是 Timer的>Interval是以毫秒为单位的,这使得它的最大值远小于30分钟.通过将间隔设置为60000",计时器将每 1 分钟滴答"一次,并且在每次滴答时,您将增加跟踪计时器滴答总数的变量.当该变量的值达到 30(表示 30 分钟已过)时,您将其重置为 0 并运行您的 ping 代码.

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.

Timer.Tick 事件.这就是我上面刚刚谈到的代码将要去的地方.

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

这篇关于如何定期 ping 服务器以确定它是否可从 VB.NET 应用程序使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆