One timer, many method calls or many timers, one method call?

I am developing an application for WinCE 5.0 on .NET CF 2.0.

I was wondering what other people consider the best solution in the following two cases:

Suppose I have 10 methods that need to be run every 50 ms.

Solution 1:

Create one System.Threading.Timer, which starts every 50 ms, and then in the callback the above 10 methods are launched.

Solution 2:

Create 10 System.Threading.Timer, which starts every 50 ms, and then calls one of the above methods in each of its callbacks.

Which solution works best? Some of the methods are intense, others are not.

+6
c # windows-ce timer
source share
5 answers

I do not think that I would use a timer. The timer is about to spawn a thread when it fires, which: a) takes longer and b) allows re-inclusion, that is, you can run your methods, especially if they take some time at the same time.

I start one thread at startup (you determine what it means - launching the application, creating an object, etc.), which performs all reads sequentially, makes a sleep call, and then repeats.

Something like that:

private void Startup() { new Thread(WorkerProc) { IsBackground = true } .Start(); } private void WorkerProc() { int pollPeriod = 50; while (true) { var et = Environment.TickCount; // call your methods et = Environment.TickCount - et; var sleep = pollPeriod - et; if (sleep < 0) sleep = 0; // always yield Thread.Sleep(sleep); } } 
+1
source share

Using multiple timers makes calls independent. This is most important with regard to exceptions. Do you want other methods to continue if # 2 throws away? Are methods otherwise dependent on each other?

With multiple timers on a multi-core processor, you will profit from execution on ThreadPool.

In Win-CE, you are probably working on a single core, which makes it part of this researcher.

+3
source share

It comes down to how accurate these methods are. Calling each method in a sequence (using the same timer) will not start all methods every 50 ms, since each method takes time to complete.

If all methods must run every 50 seconds: use different timers; otherwise use the same timer.

+1
source share

since it does not seem to depend on the order of your operations, otherwise you would not ask a question.

I would prefer the solution "1 Timer per operation". If you have an operation that again takes a lot of time (a lot of data), at least other operations will be performed. But I do not know if this will really help you. It depends on your needs / implementation.

0
source share

I would choose solution 1, at least for your intensive processor methods.

Then you implicitly run your methods sequentially. I assume that since you are on WinCE, you do not have so many cores or a large amount of RAM, and it is best not to try to run more code in parallel than necessary.

In solution 2, you run the risk of creating multiple threads that execute your 10 methods at once. This may be good if you expect I / O, especially network I / O.

0
source share

All Articles