在特定时间引发按钮的Click事件 [英] Raise button's Click event at specific time
本文介绍了在特定时间引发按钮的Click事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
如何使按钮每天使用Timer控件在晚上11:00自动单击?
Hi to all,
How can I make a Button automatically click every day at 11:00PM using a Timer control?
推荐答案
好,我真的无法想象您会遇到哪种情况确实想在此时单击一个按钮,但这是您的要求.这个问题实际上有三部分要考虑,因此我一次将其涵盖.
OK, I can''t really imagine a scenario where you would actually want to click a button at this time, but they are your requirements. There are actually three parts to consider with this question, so I''ll cover them one at a time.
- 单击按钮时,您只是在调用方法,因此您实际上不必担心按钮的一面-只需调用包含您的按钮无论如何都会触发的逻辑的方法即可.
- 运行此代码的代码应驻留在后台线程上.在我曾经编写的必须等待指定时间间隔的应用程序中,我产生了一个后台线程-后台线程然后计算了11PM与当前时间之间的时差(您可能需要做一些一天的操作才能获得正确的时间天作为您的计算基础).然后,它仅使用Monitor等待该时间间隔-如果关闭了应用程序,则Monitor被脉冲唤醒以使其可以正常退出.
- 不能保证代码的正确性在晚上11点运行它会尽其所能运行,但是没有办法保证您的代码此时会真正执行.
WinForms(您尚未指定)...
您可以在应用程序启动时查询DateTime.Now
,计算从该时间到11:00 PM的毫秒数,并将其设置为Timer
的Interval
,然后启动它.
在Tick
处理程序中,可以在有问题的Button
上调用PerformClick()
.
Assuming WinForms (as you have not specified)...
You can queryDateTime.Now
at application startup, calculate the number of milliseconds from that time to 11:00PM and set that as theInterval
of yourTimer
then start it.
In theTick
handler you can callPerformClick()
on theButton
in question.
这篇关于在特定时间引发按钮的Click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文