如何在 C# 中设置计时器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17023113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set timer in C#?
提问by Shajee Afzal
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.
我正在用 C# 编写一个程序,它会在每 15 分钟后 ping“google”。如果 ping 成功,它会在 15 分钟后再次检查(ping),依此类推...如果 ping 不成功,它将执行我的 ISP 的 dailer 并在每 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.
我已经编写了所有代码,但似乎无法将计时器设置为每 15 分钟重复一次代码。如果有人能帮助我,我将不胜感激。
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).
这段代码的作用是,如果我的拨号器在我执行这个程序时已经连接了——它什么都不做。4 秒后甚至不重新检查。但是如果我运行这个程序时拨号器没有连接,它会立即连接我的拨号器,并在每 4 秒后尝试重新连接拨号器,甚至不检查(ping 谷歌)。
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.
问候,沙吉A。
采纳答案by p.s.w.g
It sounds like you just need to move your ping code inside your timer's Tickhandler. Like this:
听起来您只需要将 ping 代码移动到计时器的Tick处理程序中即可。像这样:
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");
}
}
回答by Pruyque
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.
如果 ping 失败,则将 timer1_Tick 方法连接到委托,如果成功则不连接它。委托每 4 秒调用一次。因此,如果第一个测试失败,则每 4 秒调用一次 dailer,如果失败,则不会发生任何事情。
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.
您需要将 time1_Tick 方法连接到委托(无测试)并将测试(ping)放置在 timer1_Tick 方法中,以每 4 秒进行一次测试。

