6

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

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

Assume that i have 10 methods that i need to run every 50mS.

Solution 1:

Create one System.Threading.Timer that runs every 50mS and then in this callback run the above mentioned 10 methods.

Solution 2:

Create 10 System.Threading.Timer that runs every 50mS and then in each of their callbacks call one of the above methods.

Which solution do you think is best? Some of the methods are cpu intensive, others are not.

user653258
  • 63
  • 1
  • 3

5 Answers5

3

Using multiple timers makes the calls independent. That matters most with respect to exceptions. Do you want the other methods to proceed if #2 throws? Are the methods otherwise dependent on each other?

With multiple timers on a multi-core you will profit by executing on the ThreadPool.

Under Win-CE you are probably running on a single core, making part of this reasoning academic.

H H
  • 263,252
  • 30
  • 330
  • 514
1

It boils down to how accurate those methods needs to be. Calling each method in sequence (using the same timer) will not run all methods every 50ms, since each method takes time to complete.

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

jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • On a single core, they will never all run in a 50 ms tact. – H H Mar 10 '11 at 09:54
  • It does not matter much if the tasks get run exactly every 50mS. My question was more related to performance since this is running on a not so fast CPU (PXA270 208MHz). Is it faster to create a thread that calls all methods and then sleeps for 50mS or to use one timer or a timer for each method, performance wise? – user653258 Mar 10 '11 at 10:36
1

I don't think I'd use a Timer at all. A Timer is going to spawn a thread when it fires, which a) takes more time and b) allows for reentrancy, meaning you could be running your methods, especially if they take a while, simultaneously.

I'd spawn a single thread at startup (you define what that means - app startup, some object creation, etc) that does all the reads sequentially, does a Sleep call and then repeats.

Something along these lines:

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);
    }
}
ctacke
  • 66,480
  • 18
  • 94
  • 155
  • That is exactly how i did until i took a look at the System.Threading.Timer class! Except for the 50mS calibration you make. I also came to the conclusion that using a timer for each operation used more resources than the above solution. – user653258 Mar 11 '11 at 14:30
0

as it looks you dont depend on the order of your operations, otherwise you wouldnt ask the question.

I would prefer the "1 Timer per operation" solution. If you have an operation which is once more time consuming (lot of data whatever) at least the other operations will still get executed. But I dont know if that really helps you. It depends a lot about your needs/implementation

fixagon
  • 5,506
  • 22
  • 26
0

I would go for Solution 1, at least for your CPU intensive methods.

Then you implicitly run your methods in sequence. I assume that since your are on WinCE you don't have that many cores or that much RAM and the best trade of is to not try to run more code in parallel than necessary.

In Solution 2 you run into the risk of creating multiple thread executing your 10 methods at the same time. This might be good if you are waiting on I/O, especially network I/O.

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108