计时器C#几小时后停止打勾?

时间:2014-01-22 05:21:53

标签: c# timer

我已设置计时器以检查网站如下:

bool stopped = true;

 private void timer1_Tick(object sender, EventArgs e)
       {
           timer1.Stop();

           if (!stopped)
           webBrowser1.Navigate("http://www.google.com");

       }

但几个小时后计时器停止滴答?任何帮助请让计时器不停!如果停止迫使它重新开始。提前谢谢大家。

2 个答案:

答案 0 :(得分:0)

我没有清楚地得到问题。您想定期检查网站的可用性吗?如果是这样,您可以试试这个

public bool PingNetwork(string hostNameOrAddress)
    {
        bool pingStatus = false;

        using (Ping p = new Ping())
        {
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            byte[] buffer = Encoding.ASCII.GetBytes(data);
            int timeout = 120;

            try
            {
                PingReply reply = p.Send(hostNameOrAddress, timeout, buffer);
                pingStatus = (reply.Status == IPStatus.Success);
            }
            catch (Exception)
            {
                pingStatus = false;
            }
        }

        return pingStatus;
    }

点击内部按钮

 private void btn_Click(object sender, EventArgs e)
    {
        if(PingNetwork("www.google.com"))
        MessageBox.Show("Network Available");
        else
            MessageBox.Show("Network Not Available");

    }

希望这能帮到你!

答案 1 :(得分:0)

如果您想在00:12:00准时启动计时器,请执行一些处理时间然后重新启动计时器,您只需要计算现在和下一个00:12:00 am时段之间的差异,例如。

   static Timer timer;

static void Main(string[] args)
{
    setup_Timer();
}

static void setup_Timer()
{
    DateTime nowTime = DateTime.Now;
    DateTime oneAmTime = new DateTime(nowTime.Year, nowTime.Month, nowTime.Day, 0, 12,0, 0);

    if (nowTime > oneAmTime)
    oneAmTime = oneAmTime.AddDays(1);

double tickTime = (double)(oneAmTime - DateTime.Now).TotalMilliseconds;
timer = new Timer(tickTime);
timer.Elapsed += timer_Elapsed;
timer.Start()
}

static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    timer.Stop();
    //process code..
   setup_Timer();
}

希望它能清除你的怀疑。