如何调用一个方法,每隔2小时在C# [英] How to call a method every 2 hours in C#

查看:410
本文介绍了如何调用一个方法,每隔2小时在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何才能够调用一个方法每隔2小时在C#

How would it be possible to call a method every 2 hours in C#

例如可能使一个点击事件发生每隔2小时

For example possibly make a click event occur every 2 hours

推荐答案

定时器的 Timer.Tick事件

  1. 添加定时器组件到窗体。
  2. 设置间隔为7200000毫秒(2×60×60×1000)
  3. 订阅勾选事件。
  4. 通话,你需要调用的方法。例如:

  1. Add Timer component to your form.
  2. Set interval to 7200000 ms (2 x 60 x 60 x 1000)
  3. Subscribe to Tick event.
  4. Call the method that you need to call. For example:

private void timer1_Tick(object sender, EventArgs e)
{
   button1_Click(this, EventArgs.Empty);
}

我假设你正在使用Windows窗体。

I assumed you are using Windows Forms.

如果您正在使用WPF,有 DispatcherTimer 类。

If you are using WPF, there is DispatcherTimer class.

这篇关于如何调用一个方法,每隔2小时在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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