Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Inplement a precise timer

Inplement a precise timer

Scheduled Pinned Locked Moved C#
csharpdatabasequestion
23 Posts 13 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Luc Pattyn

    I'm not aware of any explicit support for a due time. For a 1-second resolution I would just pick some timer (Windows.Forms/Threading/Timers depending on circumstances) and use DateTime and TimeSpan classes to calculate and set the delay. For a finer resolution, I'd start with the above, then probably execute some polling loop within the final second, including a Thread.Sleep(ms) with a number of milliseconds fitting the resolution (assuming >= 50) Whatever you do, keep in mind things could go wrong in several ways: - your PC might get very busy while calculating and setting the due time, resulting in a late call; - your PC might get very busy at the due time, so the requested action may actually run somewhat later; - your PC might be switched off or crash in the mean time. Depending on your requirements a Windows scheduled task may or may not be a better option. :)

    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

    Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

    C Offline
    C Offline
    cateyes99
    wrote on last edited by
    #21

    Yep, Windows scheduled task may be a way. But it needs to be reused/consumed by other programs, so using Windows scheduled task is out of scope.

    1 Reply Last reply
    0
    • G Garth J Lancaster

      quartz.net has a number of timers, a cron type timer being amongst them - may be worth a look 'g'

      C Offline
      C Offline
      cateyes99
      wrote on last edited by
      #22

      thanks, i'll take a look at quartz.net.

      1 Reply Last reply
      0
      • A agolddog

        I think you're right on the life-critical aspect. I don't believe there is anything in .net that will give you that granularity. I don't know if my idea fits your needs, or is even a step down the right path. In the solution I was talking about, we knew every time an action was taken exactly what the next 'automatic' action would be, and exactly when it would occur. However, for us, not so time-critical. So, even though we were able to come up with a simple 'if this happens, write this event and time to the database' scenario, our server wouldn't necessarily notice that event right at the time written. But, it did turn out to be a really simple solution to a timer-type problem. I suppose in today's world, you'd just go ahead and use a Timer and tie it to the event you wanted to perform. Anyway, good luck.

        R Offline
        R Offline
        rhp8090
        wrote on last edited by
        #23

        FWIW: In a Windows Service that I wrote awhile back, I used the following: // Initiate our System Timer (set to one minute here) double dblStartupInterval = 60000; this.timProcessTimer_PollPV = new System.Timers.Timer (dblStartupInterval); // Note that everything done by the Timer is tied to // the Elapsed Event, which is raised every interval. // Instruct the Framework to call // the "ServiceTimer_PollPV_Tick" method when this // Timer elapses -- that is, when its interval runs its course. this.timProcessTimer_PollPV.Elapsed += new System.Timers.ElapsedEventHandler(this.ServiceTimer_PollPV_Tick);

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups