Re: Timers

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Sat, 14 Jun 2008 17:36:41 -0700, daveL <vettes_n_jets@xxxxxxxxx> wrote:

Im not explaining myself well , because im unsure how to approach this

so do i set the interval to x amount of milliseconds based on milliseconds
in a 24 hour period
then check if its the day i want

You could. But why not just set the interval to the number of milliseconds in a week?

also if time to run is involved do i set the timer to lets say hourly and
how to make it precise on the time required to run

How precise do you need it to be?

It seems to me your best bet is to determine the exact time in the future you want the timer to execute, calculate the difference between that exact time and "now", and then use the result of that as the timer delay for a one-shot timer.

Elapsed time calculation might look like:

DateTime dtEvent = /* initialized to some specific date/time */
TimeSpan tsDelay = dtEvent - DateTime.Now;

Then the milliseconds are "tsDelay.TotalMilliseconds". Assuming you're using a timer that requires setting in milliseconds, set this for the elapsed time for the timer and start the timer. It will fire when the elapsed time has expired. (Obviously for a timer class that accepts a TimeSpan value, you don't need to use the TotalMilliseconds property to convert to milliseconds :) ).

Each timer class has a slightly different syntax for configuring it as a one-shot timer, so without knowing what class you're using, it's not possible to say for sure what you would want to do. But, for example, the System.Timers.Timer class has an AutoReset property which, when set to false, causes the timer to fire just once. It won't fire again until you specifically start it again.

Finally, a week is a long time. Obviously you may want to include some means of re-enabling the timer in case your process exits and is restarted before the timer fires. This is just a special case of the above calculation, where the "specific date/time" is the same as it was before, but of course DateTime.Now returns a new value. :)

the thread is running perfect i just dont have it running like i need
yet....

No offense intended, but that sentence seems self-contradictory. :)

Pete
.



Relevant Pages

  • Re: VB6 code runs different speeds on different PCs
    ... As you've discovered, the minimum Sleep period can be different on different machines (5 milliseconds on my own WinXP system, but longer than that on others I think). ... You can get whatever "game rate" you want in a closed loop of course, by checking a high resolution timing source. ... If I were you I would use a proper Timer with a higher resolution that the standard VB Timer and run your entire game in the Timer event. ... Admittedly, updating their positions more than once every ten milliseconds does appear to give smoother animation (even though the video frame rate simply cannot draw a new frame at a rate greater than every 10 milliseconds,or so, depending on your display settings) because it tends iron out the differences more. ...
    (microsoft.public.vb.general.discussion)
  • Re: System.Threading.Timer period erratic
    ... | fire every. ... | the next firing is delayed by some xx milliseconds. ... | Is there something in the timer code which prevents the timer period from ... | NB I am using a system.diagnostics stopwatch to log the timer timings - so ...
    (microsoft.public.dotnet.framework)
  • System.Threading.Timer period erratic
    ... fire every. ... the next firing is delayed by some xx milliseconds. ... Is there something in the timer code which prevents the timer period from ... NB I am using a system.diagnostics stopwatch to log the timer timings - so ...
    (microsoft.public.dotnet.framework)
  • Re: Timer fires a few milliseconds before the actual Due-Time
    ... >>> execute the timer is simply generate an event, ... >> timer went off a few milliseconds too late or even seconds late but, ... >> expecting to find at least one item with an action time that has past ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: System.Threading.Timers question
    ... > System.Threading.Timers uses the number of milliseconds since the system ... > should fire, using something like timeGetTime. ... > reboot my machine, it has probably been running roughly 40 days, thus ... > therefore the timer will fire immediately. ...
    (microsoft.public.dotnet.languages.csharp)