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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. c# alarm class implementation?

c# alarm class implementation?

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpquestion
4 Posts 3 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.
  • A Offline
    A Offline
    AndrewCherry
    wrote on last edited by
    #1

    I'm writing a simple little c# class to simply function as an alarm - you pass it a System.DateTime with the constructor, and when that time is reached, it fires an "Alarm" event. Simple enough. However, what's the most efficient way of doing this? Currently, I have thought of two ways. The first has a timer within the class that raises it's event every second or so, and each time it checks to see whether the time now is the same as the target time (accurate to the minute essentially, which is all that's neccesary). The second is to work out how many milliseconds is between the time the control is enabled, and the target time, and set the interval to that. With repeat turned off, this means that in theory the timer will fire when the alarm is due, which is maybe more efficient than regular comparison checking? Are these both crazy? Which is more sensible? Is the System.Timers.Timer accurate enough to do the second option anyway? Am i missing a very easy way of doing this that is nothing like the above? Thank you very much in advance for any comments.

    M N 2 Replies Last reply
    0
    • A AndrewCherry

      I'm writing a simple little c# class to simply function as an alarm - you pass it a System.DateTime with the constructor, and when that time is reached, it fires an "Alarm" event. Simple enough. However, what's the most efficient way of doing this? Currently, I have thought of two ways. The first has a timer within the class that raises it's event every second or so, and each time it checks to see whether the time now is the same as the target time (accurate to the minute essentially, which is all that's neccesary). The second is to work out how many milliseconds is between the time the control is enabled, and the target time, and set the interval to that. With repeat turned off, this means that in theory the timer will fire when the alarm is due, which is maybe more efficient than regular comparison checking? Are these both crazy? Which is more sensible? Is the System.Timers.Timer accurate enough to do the second option anyway? Am i missing a very easy way of doing this that is nothing like the above? Thank you very much in advance for any comments.

      M Offline
      M Offline
      Matt Philmon
      wrote on last edited by
      #2

      There doesn't appear to be any clear "best" way of dealing with this kind of thing. I have a couple of Windows Services that have to do various things at "scheduled" times. At the time I needed to write them, I chose the services route. Who knows now whether another method would have worked better... but from my experience it doesn't matter much. I run a timer as you suggested. Since my timing is accurate to the minute (not second) then I fire the timer every 60 seconds. Start the timer when the application starts and let it go with that. Don't use the Forms Timer. Use one of others. They're much better (like the System.Timers or the System.Threading.Timers). Another option (which I could have taken) is to use the .NET framework wrappers for the Windows Scheduler service. There's an article on here all about that. Then you can write an application that runs when it's scheduled to and that's it. Simple as you please as long as the user is letting this service run.

      1 Reply Last reply
      0
      • A AndrewCherry

        I'm writing a simple little c# class to simply function as an alarm - you pass it a System.DateTime with the constructor, and when that time is reached, it fires an "Alarm" event. Simple enough. However, what's the most efficient way of doing this? Currently, I have thought of two ways. The first has a timer within the class that raises it's event every second or so, and each time it checks to see whether the time now is the same as the target time (accurate to the minute essentially, which is all that's neccesary). The second is to work out how many milliseconds is between the time the control is enabled, and the target time, and set the interval to that. With repeat turned off, this means that in theory the timer will fire when the alarm is due, which is maybe more efficient than regular comparison checking? Are these both crazy? Which is more sensible? Is the System.Timers.Timer accurate enough to do the second option anyway? Am i missing a very easy way of doing this that is nothing like the above? Thank you very much in advance for any comments.

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        This isn't really a framework related question and besides you have already posted the same question to the C# forum.:mad:

        A 1 Reply Last reply
        0
        • N Not Active

          This isn't really a framework related question and besides you have already posted the same question to the C# forum.:mad:

          A Offline
          A Offline
          AndrewCherry
          wrote on last edited by
          #4

          Many apologies for any annoyance caused by a double post - I couldn't decide whether this was more of a general framework question or not. I happen to implement in c#, so posted there first, but thought that then it might be more appropriate as a general question of technique. Again, apologies for offence caused!

          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