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. Knowing when the date changes with less cost...

Knowing when the date changes with less cost...

Scheduled Pinned Locked Moved C#
csshardwarehelpquestionlearning
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.
  • M Offline
    M Offline
    Mel Feik
    wrote on last edited by
    #1

    I have an application that writes any data it receives on a port to a file. The files have the date appended to them which means I have to keep an eye out for a new day to roll around. At present I'm using this little bit of code...

    DateTime today = DateTime.Now;
    //..........
    if (!(today.Date == DateTime.Now.Date))
    {
    today = DateTime.Now;
    //... do my new day stuff
    }

    ...which is straight forward enough. I am just wondering if there is a better (more efficient) means to know when the clock rolls over to a new day? As it is, I'm running that bit of code immediately before EVERY file write, and change the file name as needed. For now, its not an issue take the time to do things this way, but as my incoming data load increases I can see where streamlining is going to become an issue. Any suggestions?

    --------------------------------------------- Help... I'm embedded and I can't get out! If they don't get the basic research and learning skills down then they'll end up having a very hard life (Either that or they'll become managers) - Micheal P Butler

    R J 2 Replies Last reply
    0
    • M Mel Feik

      I have an application that writes any data it receives on a port to a file. The files have the date appended to them which means I have to keep an eye out for a new day to roll around. At present I'm using this little bit of code...

      DateTime today = DateTime.Now;
      //..........
      if (!(today.Date == DateTime.Now.Date))
      {
      today = DateTime.Now;
      //... do my new day stuff
      }

      ...which is straight forward enough. I am just wondering if there is a better (more efficient) means to know when the clock rolls over to a new day? As it is, I'm running that bit of code immediately before EVERY file write, and change the file name as needed. For now, its not an issue take the time to do things this way, but as my incoming data load increases I can see where streamlining is going to become an issue. Any suggestions?

      --------------------------------------------- Help... I'm embedded and I can't get out! If they don't get the basic research and learning skills down then they'll end up having a very hard life (Either that or they'll become managers) - Micheal P Butler

      R Offline
      R Offline
      realJSOP
      wrote on last edited by
      #2

      You could set up a timer event (or a BackgroundWorker object) that trips at midnight. The only semi-tricky part is determining the first interval to fire at (midnight - the current time), and then for every other following interval, just add 1 day to the current time. If you setup a BackgroundWorker object to do this, you should make sure it supports cancellation, and put in a loop that checks the time every few seconds (so it can respond to the cancellation request).

      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
      -----
      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

      1 Reply Last reply
      0
      • M Mel Feik

        I have an application that writes any data it receives on a port to a file. The files have the date appended to them which means I have to keep an eye out for a new day to roll around. At present I'm using this little bit of code...

        DateTime today = DateTime.Now;
        //..........
        if (!(today.Date == DateTime.Now.Date))
        {
        today = DateTime.Now;
        //... do my new day stuff
        }

        ...which is straight forward enough. I am just wondering if there is a better (more efficient) means to know when the clock rolls over to a new day? As it is, I'm running that bit of code immediately before EVERY file write, and change the file name as needed. For now, its not an issue take the time to do things this way, but as my incoming data load increases I can see where streamlining is going to become an issue. Any suggestions?

        --------------------------------------------- Help... I'm embedded and I can't get out! If they don't get the basic research and learning skills down then they'll end up having a very hard life (Either that or they'll become managers) - Micheal P Butler

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #3

        If you look at the Logging Enterprise Library from Microsoft, one of the TraceListeners it supports is a "RollingFlatFileTraceListener" - this has a setting to make it roll over to a new file at midnight (as well as other options like at xxxKb filesize)

        M 1 Reply Last reply
        0
        • J J4amieC

          If you look at the Logging Enterprise Library from Microsoft, one of the TraceListeners it supports is a "RollingFlatFileTraceListener" - this has a setting to make it roll over to a new file at midnight (as well as other options like at xxxKb filesize)

          M Offline
          M Offline
          Mel Feik
          wrote on last edited by
          #4

          Thanks for that... I didnt even know the Enterprise Library existed. I have been taking a look at on off moments the past couple days, learning curve is a bit high for me to make use of it for what I'm presently working on; however, it is something I can make good use of in the near future (much more than just the file listener at that!).

          --------------------------------------------- Help... I'm embedded and I can't get out! If they don't get the basic research and learning skills down then they'll end up having a very hard life (Either that or they'll become managers) - Micheal P Butler

          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