简体   繁体   English

什么是实现线程计时器的最佳方法

[英]what is the best approach for implementing a thread timer

What is the best approach in using a timer. 使用计时器的最佳方法是什么? Use a System.Timer.Timer class or use a single Thread in a non-terminating loop with a Thread.Sleep statement? 使用System.Timer.Timer类还是在非终止循环中使用Thread.Sleep语句使用单个Thread?

Thanks in advance 提前致谢

In general, use the components that are already there if they serve your needs. 通常,如果可以满足您的需求,请使用已经存在的组件。 However, System.Threading.Timer uses the .NET thread pool, so any of the following conditions would make it a poor candidate: 但是, System.Threading.Timer使用.NET线程池,因此以下任何一种情况都将使其不适合使用:

  • You require a STA thread (all ThreadPool threads are MTA) 您需要一个STA线程(所有ThreadPool线程都是MTA)
  • You require that all occurrences of your repeated task run on the same thread 您要求所有重复任务的出现都在同一线程上运行
  • You want to assign a particular priority (lower or higher) to your tasks 您要为任务分配特定的优先级(较低或较高)
  • Your tasks are particularly long-running or utilize non-trivial blocks 您的任务特别长时间运行或利用非平凡的块

Use a Timer. 使用计时器。 It's there, specifically for that purpose, so why wouldn't you use it? 它在那里,专门为此目的,所以您为什么不使用它呢?

The two methods you refer to are used for different results. 您引用的两种方法用于不同的结果。

Timers will fire the event and invoke your method on a scheduled interval. 计时器将触发事件并按计划的时间间隔调用您的方法。 They could invoke your method whilst another instance of it is running unless you stop the timer at the start of your processing (DoWork) and start it again when you're done (but then you might miss the timed events). 他们可以在另一个实例正在运行时调用您的方法,除非您在处理开始时停止计时器(DoWork),然后在完成后再次启动它(但是这样您可能会错过定时事件)。

A method that loops and sleeps will not be invoked when it's busy. 忙碌时不会调用循环和睡眠的方法。 The "advantage" here is that you can DoWork, then find that the next timer event has already passed and DoWork immediately again. 这里的“优势”是您可以执行DoWork,然后发现下一个计时器事件已经过去,并且立即再次执行DoWork。 The alternative is that you have rest periods where you sleep a specified amount of time regardless of how long your DoWork method took. 另一种选择是,无论您的DoWork方法花费了多长时间,您都会有一段休息时间,在这段时间内您会睡一段指定的时间。

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

相关问题 在C#中实现monad的最佳方法是什么 - What is the best approach to implementing monads in C# 实现线程安全字典的最佳方法是什么? - What's the best way of implementing a thread-safe Dictionary? 与Thread.Sleep相比,在C#中暂停线程可获得更准确的时间-最佳方法是什么? - Pausing the thread in C# for more accurate time than Thread.Sleep - what would be the best approach? 在C#中使函数或语句集线程安全的最佳方法是什么? - What is a best approach to make a function or set of statements thread safe in C#? 安排活动的最佳方法是什么? - What is the best approach to schedule events? 什么是自动增量的最佳方法 - What is Best Approach for Auto Increament SerialPort 上多线程的最佳方法是什么 - What is a best approach for multithreading on SerialPort 制作DAL的最佳方法是什么? - What is the best approach to make DAL? plot 图的最佳方法是什么? - What is the best approach to plot graphs? 在C#应用程序的ArrayList对象上对线程安全的多个并行读取器线程和偶发写入器线程进行线程安全的最佳方法是什么? - What is the best approach to thread-safe multiple parallel readers threads and occasional writer thread on an ArrayList object to a C# application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM