Which timer is better

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zaferaydin
    New Member
    • Aug 2009
    • 9

    #1

    Which timer is better

    Hi,
    I have many cyclic operations in my windows application. These operations must be called in a period of 1 second. And this operations must be run underground.

    in .Net there are Windows.Forms.T imer, System.Threadin g.Timer and a Thread function running a loop with Sleep(1000) inside. Now i want to decide which one is better to use.

    1. Creating System.Windows. Forms.Timer
    2. Creating System.Threadin g.Timer
    3. Creating a function like :
    Code:
    void StartMyTimer()
    {
     Thread thread = new Thread(new ThreadStart(CheckTask));
     thread.IsBackground = true;
     thread.Start();
    }
    void CheckTask()
    {
     while(true)
     {
       // do job 
      Thread.Sleep(1000);
     }
    }
    Which one is the efficient way?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Depends on your particular need.

    System.Threadin g.Timer is roughly what your function is doing. It runs in a seperate thread and calls out the function at its interval time.

    System.Windows. Forms.Timer does not use different threads and relies on the windows messaging loop to provide is "tick" callback.

    There is also System.Timers.T imer, but that is roughly the same as the windows.forms version, just made for a non-gui.

    Neither are garunteed to happen at exactly at 1 second (read up on realtime OSs for more info on that), but they will not happen prior to 1 second.

    Comment

    • zaferaydin
      New Member
      • Aug 2009
      • 9

      #3
      Thanks Plater,

      I think 3rd one satisfy my needs. If there is not performance difference between them, then it is better to use my current technique which is number 3.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Well I would hope they implemented their Sytem.Threading .Timer in the most efficient way(but maybe not), it probably has more overhead then you need and is trickier to follow. If the loop works for you, go with it. Just make sure you have a way to break out of it, or your thread might get stuck open.

        Comment

        Working...