简体   繁体   English

这么多秒后如何调用方法?

[英]How to call a method after so many seconds?

In my project I would like to be able to call a method after 3 minutes have passed. 在我的项目中,我希望能够在3分钟后调用一个方法。 There is no "main loop" to speak of so I cannot use a stopwatch and constantly check if 3 minutes have passed. 没有所谓的“主循环”,所以我无法使用秒表来不断检查是否经过了3分钟。 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. 我能想到的唯一其他解决方案是间隔为3分钟的Timer对象,但是这看起来很混乱,因为在3分钟的延迟之后它只会被调用一次。

Thanks for reading. 谢谢阅读。

Edit: 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. 我真的相信您对Timer对象的想法是正确的方法。 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 http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx

if you have hundreds or thousand of those timers, some scheduling will do the job. 如果您有成百上千的计时器,则可以进行一些调度。

You really should investigate quartznet 你真的应该研究石英网

lots of concurrent timers perform not very well as i experienced, quartznet will do. 许多并发计时器的执行效果都不如我所经历的好,quartznet会做到。

and if you really want to have those scheduling tasks in your application, perhaps this article gives you some ideas Task Scheduler Class Library for .NET 如果您确实希望在应用程序中拥有这些计划任务,那么也许本文可以为您提供一些想法。.NET的任务计划程序类库

Use a Timer. 使用计时器。

While there will be a slight overhead if you create a 1000 timers, it won't be too much. 如果您创建1000个计时器,虽然会有一些开销,但不会太多。 Timers will do their job and fire an event when 3 minutes have passed. 计时器将在3分钟后完成工作并触发事件。 Also, if you want your code to repeat every 3 minutes, Timers are the way to go. 另外,如果您希望代码每3分钟重复一次,则可以使用计时器。

Timer is not a good solution for hundreds/thousands, as Timers are a limited resource. 计时器不是成百上千的好解决方案,因为计时器是有限的资源。

Are all the methods to be called from the same application? 是否要从同一应用程序调用所有方法?

If so, I would create a small stub class that had a datetime, and an Action, and add delegates to the method to be called, along with the target time. 如果是这样,我将创建一个具有日期时间和Action的小型存根类,并将委托与目标时间一起添加到要调用的方法中。 Then in a background thread loop through the list and call the delegates when appropriate (and remove them obviously) 然后在后台线程中循环遍历该列表,并在适当时调用这些委托(并显然将其删除)

An alternate solution would be to do something like QueueUserWorkItem for each method to be called, and first thing in the thread sleep for the appropriate amount of time until the method should be called. 另一种解决方案是对每个要调用的方法执行类似QueueUserWorkItem的操作,并且线程中的第一件事将休眠适当的时间,直到应调用该方法为止。

For all solutions (mine and others), be aware of any marshaling you have to do if you are interacting with the GUI, or any locking that may need to be done to avoid threading concurrency issues 对于所有解决方案(我的解决方案和其他解决方案),请注意在与GUI交互时必须进行的封送处理,或者可能需要进行的任何锁定以避免线程并发问题

After trying out several options, the method was not self-triggering. 在尝试了几种选择之后,该方法不是自触发的。 Hence, I used Javascript to solve the same problem as shown below. 因此,我使用Javascript解决了如下所示的相同问题。

<script>
function Timer() {
    $.ajax({
        url: "@Url.Action("Timer", "Exam")",//Method to call. Timer Method in Exam Controller
        type: 'GET', // <-- make a async request by GET
    dataType: 'html', // <-- to expect an html response
    cache: false,
    async: true
    });
}
setInterval(function () { Timer(); }, 5000);//Triggers method Timer() after 5 seconds
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM