如何调用一个方法,每隔2小时在C# [英] How to call a method every 2 hours in C#
本文介绍了如何调用一个方法,每隔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事件
- 添加
定时器
组件到窗体。 - 设置间隔为7200000毫秒(2×60×60×1000)
- 订阅
勾选
事件。 -
通话,你需要调用的方法。例如:
- Add
Timer
component to your form. - Set interval to 7200000 ms (2 x 60 x 60 x 1000)
- Subscribe to
Tick
event. 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屋!
查看全文