简体   繁体   English

每 20 秒调用一次方法的最有效方法是什么

[英]What's the most efficient way to call a method every 20 seconds

I would like to call a method passing a parameter every 20 seconds, eg public void ProcessPerson(IPerson person)我想调用一个每 20 秒传递一个参数的方法,例如 public void ProcessPerson(IPerson person)

I've been reading through the different Timer options and was wondering if anybody could recommend the most efficient way to do this?我一直在阅读不同的 Timer 选项,想知道是否有人可以推荐最有效的方法来做到这一点?

In addition, is there a way to keep the parameter strongly typed rather than use object and then have to convert to IPerson?另外,有没有办法保持参数强类型而不是使用对象然后必须转换为IPerson?

Thank you for your help.谢谢您的帮助。

A System.Threading.Timer is what you need: System.Threading.Timer正是您所需要的:

Provides a mechanism for executing a method at specified intervals.提供一种以指定时间间隔执行方法的机制。

Use a TimerCallback delegate to specify the method you want the Timer to execute.使用 TimerCallback 委托指定您希望 Timer 执行的方法。 The timer delegate is specified when the timer is constructed, and cannot be changed.计时器委托是在构造计时器时指定的,并且无法更改。 The method does not execute on the thread that created the timer;该方法不会在创建计时器的线程上执行; it executes on a ThreadPool thread supplied by the system.它在系统提供的 ThreadPool 线程上执行。

There's also the System.Windows.Forms.Timer :还有System.Windows.Forms.Timer

Implements a timer that raises an event at user-defined intervals.实现以用户定义的时间间隔引发事件的计时器。 This timer is optimized for use in Windows Forms applications and must be used in a window.此计时器针对在 Windows 窗体应用程序中使用进行了优化,并且必须在窗口中使用。

This Windows timer is designed for a single-threaded environment where UI threads are used to perform processing.此 Windows 计时器专为使用 UI 线程执行处理的单线程环境而设计。

And don't forget System.Timers.Timer :并且不要忘记System.Timers.Timer

The Timer component is a server-based timer, which allows you to specify a recurring interval at which the Elapsed event is raised in your application. Timer 组件是一个基于服务器的计时器,它允许您指定在应用程序中引发 Elapsed 事件的重复间隔。

The server-based Timer is designed for use with worker threads in a multithreaded environment.基于服务器的 Timer 设计用于多线程环境中的工作线程。 Server timers can move among threads to handle the raised Elapsed event, resulting in more accuracy than Windows timers in raising the event on time.服务器计时器可以在线程之间移动以处理引发的 Elapsed 事件,从而在按时引发事件方面比 Windows 计时器更准确。

So investigate each and decide which one works best in your case.因此,调查每一项并决定哪一项最适合您的情况。

I would go for System.Threading.Timer .我会去System.Threading.Timer Keep in mind that Windows is not a real-time OS system so there will be some variance on the timing no matter what mechanism you choose.请记住,Windows 不是实时操作系统系统,因此无论您选择何种机制,时间都会有所不同。

You did not say if this function will be performing any kind of UI updates, but just in case you should also know that the callback function will be executed on a different thread so in some cases like performing UI updates you will need to take the appropriate action to marshal the request back to the UI thread.你没有说这个函数是否会执行任何类型的 UI 更新,但以防万一你还应该知道回调函数将在不同的线程上执行,所以在某些情况下,比如执行 UI 更新,你需要采取适当的将请求封送回 UI 线程的操作。

To marshal the request back to the UI thread you can do the following要将请求编组回 UI 线程,您可以执行以下操作

For WinForms you can use Control.Invoke对于 WinForms,您可以使用Control.Invoke

For WPF you can use Dispatcher.Invoke对于 WPF,您可以使用Dispatcher.Invoke

Or the Async variant BeginInvoke或者 Async 变体 BeginInvoke

You can use this, just make sure whatever you are doing is thread safe.你可以使用它,只要确保你在做什么是线程安全的。


using System.Threading;

public void DoStuff(IPerson person)
{
    new Timer(ProcessPerson, person, 0, 20000);
}

public void ProcessPerson(object threadState)
{
   IPerson person = threadState as IPerson;
   // do your stuff
}

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

相关问题 清除数组的最有效方法是什么? - What's the most efficient way to clear an array? 生成缩略图的最有效方法是什么? - What's the most efficient way to generate thumbnails? 按字母顺序排序的最有效方法是什么? - What's the most efficient way to sort this alphabetically? 什么是在某种条件下拦截和忽略方法调用的最有效方法? - What would be the most efficient way to intercept and ignore a method call on a certain condition? 最有效的方法是获取在C#中采用任何类型的可索引列表的方法 - What's the most efficient way to have a method that takes an indexable list of any type in c# 将行为注入我所有实体的最有效方法是什么? - What's the most efficient way to inject behavior into all my entities? 在XDocument中查找和设置元素值的最有效方法是什么? - What's the most efficient way to locate and set element values in an XDocument? 什么是将DataTable转换为对象的最有效方法[,]? - What's the most efficient way to convert a DataTable to an object[,]? 访问.html页面的最有效方法是什么? - What's the most efficient way to visit a .html page? 加载用户的DirectoryEntry的最有效方法是什么? - What is the most efficient way to load a user's DirectoryEntry?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM