最好安排在C#中的任务的方式
我正在开发,需要能够调度各种异步任务在不同的时间C#程序。我需要一个解决方案,激活每一秒钟检查任务的执行,并吃掉了尽可能少的资源成为可能。这也需要很好地扩展到了大量的任务。哪一项是更好地使用,或者是有什么?
I'm developing a C# program that needs to be able to schedule a variety of asynchronous tasks at different times. I need a solution that activates every second to check for tasks to execute, and eats up as few resources as possible. It also needs to scale well to a large number of tasks. Which of these is better to use, or is there any ?
1)使用与1秒的总时间System.Timers.Timer的目的在于,触发执行任务的事件,或者
1) Using a System.Timers.Timer object with a 1 second elapsed time to trigger an event that executes tasks, or
2)使用休眠1秒一个单独的线程,然后执行任务时就醒来,或
2) Using a separate thread that sleeps for 1 second and then executes tasks when it wakes up, or
3)完全是另一回事。
3) Something else entirely.
System.Threading.Timer
是极其轻便。你可以为每个任务一个单独的计时器,这样计时器来,由于任务的时候应该执行。
System.Threading.Timer
is extremely lightweight. You could create a separate timer for each task so that the timer comes due when the task is supposed to execute.
在内部,我认为,该系统会跟踪哪些定时器是由于旁边,因此只需要监控一个定时器,在任何给定时间(source/similar问题)。
Internally, I believe that the system will keep track of what timer is due next so it only has to monitor one timer at any given time (source/similar question).