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