System.Timers.Timer 如何获取 Elapse 之前的剩余时间
使用 C#,如何从 System.Timers.Timer 对象获取剩余时间(在 elapse 事件发生之前)?
换句话说,假设我将计时器间隔设置为 6 小时,但 3 小时后,我想知道还剩多少时间。我如何让计时器对象显示剩余时间?
Using C#, how may I get the time remaining (before the elapse event will occur) from a System.Timers.Timer
object?
In other words, let say I set the timer interval to 6 hours, but 3 hours later, I want to know how much time is remaining. How would I get the timer object to reveal this time remaining?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
内置计时器不提供剩余时间。您需要创建自己的类来包装计时器并公开此信息。
像这样的东西应该有效。
The built-in timer doesn't provide the time remaining until elapse. You'll need to create your own class which wraps a timer and exposes this info.
Something like this should work.
我知道这个话题已经有3年多了。然而,我在解决完全相同的问题时遇到了它。
受 Samuel Neff 的启发,我通过扩展标准 System.Timers.Timer 类提出了一个无 WinForms 的解决方案:
我希望它有所帮助。
I am aware, that the topic is more than 3 years old. However I came across it while tackling exactly the same problem.
Inspired by Samuel Neff, I came up with a WinForms-less solution by extending the standard System.Timers.Timer class:
I hope it helps.
我想最好的方法是将开始时间保存在变量中,然后计算经过的时间:
I guess the best method is to hold the start time in a variable and then calculate the elapsed time as