如何调用这么多秒后一种方法? [英] How to call a method after so many seconds?

查看:108
本文介绍了如何调用这么多秒后一种方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目,我想能够调用方法后的3分钟过去了。没有主循环可言,所以我不能用秒表不断检查如果在三分钟过去了。唯一的其他解决方案,我能想到的是用3分钟间隔的Timer对象,但似乎相当混乱,因为它只会进行一次3分钟的延迟之后调用。

In my project I would like to be able to call a method after 3 minutes have passed. There is no "main loop" to speak of so I cannot use a stopwatch and constantly check if 3 minutes have passed. The only other solution I can think of is a Timer object with an interval of 3 minutes but that seems rather messy because it will only be called once after the 3 minute delay.

感谢您的阅读

编辑:忘了提。这是一个服务器应用程序,所以我不能暂停线程的执行,因为其他的东西将在此期间进行处理。此外,该定时器机制将不会孤单。可能有几百甚至上千并发计时器在一个时间。

Forgot to mention. This is for a server application so I cannot pause the execution of the thread because other stuff will have to be handled in the meantime. Also, this timer mechanism will not be alone. There may be hundreds of even thousands of concurrent timers at a time.

推荐答案

我真的相信你的想法定时目标是正确的道路要走。这听起来像你熟悉如何使用它 - 但这里有一个例子:

I really believe your idea with the Timer object is the right way to go. It sounds like you're familiar with how to use it - but here's an example:

    aTimer = new System.Timers.Timer(1000 * 60 * 3);
    aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
    aTimer.Enabled = true; 

http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx

这篇关于如何调用这么多秒后一种方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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