How to set timer in C#?

假装没事ソ 提交于 2019-12-24 03:58:09

问题


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.

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.

This is the code.

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");
    }

}
}

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.

Regards, Shajee A.


回答1:


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");
    }
}



回答2:


If the ping fails, you connect the timer1_Tick method to the delegate, if it succeeds you don't connect it. The delegate is called every 4 seconds. So if the first test fails, the dailer is called every 4 seconds, if it fails nothing happens.

You'll need to connect the time1_Tick method to the delegate (without test) and place the test (ping) inside the timer1_Tick method to do the test every 4 seconds.



来源:https://stackoverflow.com/questions/17023113/how-to-set-timer-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!