NextPVR Forums
  • ______
  • Home
  • New Posts
  • Wiki
  • Members
  • Help
  • Search
  • Register
  • Login
  • Home
  • Wiki
  • Members
  • Help
  • Search
NextPVR Forums Public Developers v
« Previous 1 … 16 17 18 19 20 … 93 Next »
Timed action using C#

 
  • 0 Vote(s) - 0 Average
Timed action using C#
bgowland
Offline

Posting Freak

West Yorkshire, UK
Posts: 4,583
Threads: 384
Joined: Dec 2004
#1
2011-02-10, 12:31 AM
I need to run a particular bit of code in a System Service every hour shortly after the hour (~5 mins but not too critical).

I'm doing this with a System.Timers.Timer and setting it to fire every 3600000 ms which is working but I've noticed in 12 hours, it has drifted forwards by over 1 minute (in particular between the ~4pm -> ~5pm calls)...

Code:
09/02/2011 11:05:46.385 onTimerElapsed called...
09/02/2011 12:05:45.855 onTimerElapsed called...
...
09/02/2011 16:05:43.735 onTimerElapsed called...
09/02/2011 17:04:30.159 onTimerElapsed called...
...
09/02/2011 23:04:25.535 onTimerElapsed called...
I could check the 'Minute' part of DateTime.Now each time it's called and recalculate the next delay but I was just wondering if there's anything more accurate for doing this.

I'm sure I've done something similar in the past but can't think of how I did it and Google seems to just throw up examples of System.Timers.Timer.
psycik
Offline

Posting Freak

Posts: 5,210
Threads: 424
Joined: Sep 2005
#2
2011-02-10, 02:07 AM
I'd set the timer shorter, like to between 10 and 30 mins and check against a variable. Kind of like what you were saying, check the Minute value.

I've never found, or needed any more accuracy than that.
UncleJohnsBand
Offline

Posting Freak

U.S.A.
Posts: 5,643
Threads: 258
Joined: Feb 2005
#3
2011-02-10, 02:50 AM
I wrote a Titan Tv remote service interface once and I set a timer using a check value set in the config file for the service....then converted it to milliseconds and then use the thread sleep in a looping routine.....

Code:
//Pull user set value from config file
                checkHr = ConfigSettings.ReadSetting("CheckIntervalHr");
                checkMin = ConfigSettings.ReadSetting("CheckIntervalMin");
                checkSec = ConfigSettings.ReadSetting("CheckIntervalSec");
                
                //convert user sleep factor to milliseonds
                sleepFactor = Convert.ToInt32(checkHr)*360;
                sleepFactor += Convert.ToInt32(checkMin)*60;
                sleepFactor += Convert.ToInt32(checkSec);
                sleepFactor = sleepFactor*1000;

                //At end of loop routine within the service sleep the thread based on
                //calculated sleepFactor.....was always very precise for me
                
                Thread.Sleep(sleepFactor);
Intel Core i7 @ 4.00GHz Skylake 14nm
ASUSTeK COMPUTER INC. Z170-DELUXE
Windows 10 Pro x64
PVR Software: NPVR 5.1.1
SiliconDust HDHomeRun HDHR5-4US Connect Quatro 4 Channel Tuner
Roku Ultra
2 PCH A-100's
bgowland
Offline

Posting Freak

West Yorkshire, UK
Posts: 4,583
Threads: 384
Joined: Dec 2004
#4
2011-02-10, 11:35 PM
Thanks guys,

psycik Wrote:I'd set the timer shorter, like to between 10 and 30 mins and check against a variable. Kind of like what you were saying, check the Minute value.

I've never found, or needed any more accuracy than that.
I've decided I need a 30 minute check anyway so I'll just adjust the 'minute' if it drifts.

UncleJohnsBand Wrote:I wrote a Titan Tv remote service interface once and I set a timer using a check value set in the config file for the service....then converted it to milliseconds and then use the thread sleep in a looping routine....
Now you mention it, Thread.Sleep() is probably what I was thinking of - I've used it in the past and it was always pretty consistent. I'd need to rework my code a bit though so perhaps I'll try that in my next version (plenty of other stuff to tie up at the moment).

CHeers,
Brian
« Next Oldest | Next Newest »

Users browsing this thread: 1 Guest(s)



  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Designed by D&D, modified by NextPVR - Powered by MyBB

Linear Mode
Threaded Mode