如何在 C# 中设置计时器? [英] How to set timer in C#?

查看:54
本文介绍了如何在 C# 中设置计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C# 编写一个程序,它会在每 15 分钟后 ping google".如果 ping 成功,它会在 15 分钟后再次检查(ping),依此类推...如果 ping 不成功,它将执行我的 ISP 的 dailer 并在每 15 分钟后再次检查.

I am making a program in C# that will ping "google" after every 15 minutes. If the ping is successful, it will then again check(ping) after 15 minutes and so on...If the ping is not successful, it will execute my ISP's dailer and will check again after every 15 minutes.

我已经编写了所有代码,但我似乎无法将计时器设置为每 15 分钟重复一次代码.如果有人能帮助我,我将不胜感激.

I have written all the code but I can't seem to set the timer to repeat the code after every 15 minutes. If someone could help me, I would really appreciate it.

这是代码.

using System;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Net;
using System.Diagnostics;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        timer.Interval = (4000); //For checking, I have set the interval to 4 sec. It actually needs to be 15 minutes.
        timer.Enabled = true; 
        timer.Start(); 

        Ping ping = new Ping();

        PingReply pingStatus = ping.Send(IPAddress.Parse("208.69.34.231"));

        if (pingStatus.Status != IPStatus.Success)
        {
            timer.Tick += new EventHandler(timer1_Tick);
        }

    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        Process.Start("C:\\WINDOWS\\system32\\rasphone.exe","-d DELTA1");
    }

}
}

这段代码的作用是,如果我的拨号器在我执行这个程序时已经连接了——它什么都不做.4 秒后甚至不重新检查.但是如果我运行这个程序时拨号器没有连接,它会立即连接我的拨号器,并在每 4 秒后尝试重新连接拨号器,甚至不检查(ping 谷歌).

What this code does is, if my dialer is already connected when I execute this program - it does nothing. Doesn't even recheck after 4 seconds. But if the dialer is not connected when I run this program, it connects my dialer instantly and tries to reconnect dialer after every 4 seconds without even checking(pinging google).

我似乎无法正确设置计时器,因为我以前从未使用过计时器功能.如果有人能帮助我,我将不胜感激.

I just can't seem to set the timer properly as I have never used the timer function before. I would really appreciate if someone could help me out.

问候,沙吉A.

推荐答案

听起来您只需将 ping 代码移动到计时器的 Tick 处理程序中即可.像这样:

It sounds like you just need to move your ping code inside your timer's Tick handler. Like this:

private void Form1_Load(object sender, EventArgs e)
{
    timer.Interval = 4000;
    timer.Enabled = true; 
    timer.Tick += new EventHandler(timer1_Tick);
    timer.Start(); 
}

private void timer1_Tick(object sender, EventArgs e)
{
    Ping ping = new Ping();
    PingReply pingStatus = ping.Send(IPAddress.Parse("208.69.34.231"));

    if (pingStatus.Status != IPStatus.Success)
    {
        Process.Start("C:\\WINDOWS\\system32\\rasphone.exe","-d DELTA1");
    }
}

这篇关于如何在 C# 中设置计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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