Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

I would like to call a method passing a parameter every 20 seconds, e.g. 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?

In addition, is there a way to keep the parameter strongly typed rather than use object and then have to convert to IPerson?

Thank you for your help.

like image 714
CiaraM Avatar asked May 24 '10 14:05

CiaraM


People also ask

How to run a method every 10 seconds in Android?

This example demonstrates how do I run a method every 10 seconds in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do you call a method periodically in C#?

1 Answer. Show activity on this post. Timer is the simplest and easiest way. There are at least a million ways of solving this, some are better for some scenarios - I assumed OP wanted to have the simplest and shortest one, and in my opinion that is the Timer.


2 Answers

A System.Threading.Timer is what you need:

Provides a mechanism for executing a method at specified intervals.

Use a TimerCallback delegate to specify the method you want the Timer to execute. 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.

There's also the 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.

This Windows timer is designed for a single-threaded environment where UI threads are used to perform processing.

And don't forget 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.

The server-based Timer is designed for use with worker threads in a multithreaded environment. 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.

So investigate each and decide which one works best in your case.

like image 76
ChrisF Avatar answered Sep 17 '22 10:09

ChrisF


I would go for 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.

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.

To marshal the request back to the UI thread you can do the following

For WinForms you can use Control.Invoke

For WPF you can use Dispatcher.Invoke

Or the Async variant BeginInvoke

like image 23
Chris Taylor Avatar answered Sep 17 '22 10:09

Chris Taylor