tags:

views:

3159

answers:

5

I have an app that needs to check a database table every minute. The table is indexed by the time of day and so the app needs to run this check every minute.

What's the best of way of doing this? I can create a background worker thread but if I set it to sleep for 60 secs after each check I will eventually miss a minute because of the overhead of calling the check.

Do I remember the minute I checked and then check, every 15 secs say and if the minute has changed performed the check then.

Or is there some other approach I should use?

I'm using WPF, VS2008 and VB.NET

TIA,

Simon

+11  A: 

The DispatchTimer is what you're after - just set the interval you want, and then attach a method to the Tick event - works a treat.

MrTelly
So it does! Thanks a lot.
Simon Temlett
A: 

Not sure about WPF, but in WinForms, there's a Timer control for this. If there isn't one, one way is the following loop:

  • Check if we're past the last minute set
  • If not, sleep for a short time and check again
  • Do stuff
  • Check the current time
  • Save the minute
  • Sleep for 60000ms - current time(sec and ms part) - some value
lc
A: 

System.Timers.Timer() seems to fit your bill, using a worker thread from the pool without you having to mess with threads. You can run the VB example from the page and check for accuracy.

Imports System
Imports System.Timers

Public Class Timer1

Private Shared aTimer As System.Timers.Timer

Public Shared Sub Main()
    ' Normally, the timer is declared at the class level,
    ' so that it stays in scope as long as it is needed.
    ' If the timer is declared in a long-running method,  
    ' KeepAlive must be used to prevent the JIT compiler 
    ' from allowing aggressive garbage collection to occur 
    ' before the method ends. (See end of method.)
    'Dim aTimer As System.Timers.Timer

    ' Create a timer with a ten second interval.
    aTimer = New System.Timers.Timer(10000)

    ' Hook up the Elapsed event for the timer.
    AddHandler aTimer.Elapsed, AddressOf OnTimedEvent

    ' Set the Interval to 2 seconds (2000 milliseconds).
    aTimer.Interval = 2000
    aTimer.Enabled = True

    Console.WriteLine("Press the Enter key to exit the program.")
    Console.ReadLine()

    ' If the timer is declared in a long-running method, use
    ' KeepAlive to prevent garbage collection from occurring
    ' before the method ends.
    'GC.KeepAlive(aTimer)
End Sub

' Specify what you want to happen when the Elapsed event is 
' raised.
Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
    Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime)
End Sub
End Class

' This code example produces output similar to the following:
'
'Press the Enter key to exit the program.
'The Elapsed event was raised at 5/20/2007 8:42:27 PM
'The Elapsed event was raised at 5/20/2007 8:42:29 PM
'The Elapsed event was raised at 5/20/2007 8:42:31 PM
'...
gimel
I disagree with the usage of this specific task. If you need something to happen on the UI thread, the general practise is to use the DispatcherTimer
Ray Booysen
In fact if you tried to modify a WPF element on the callback, you'll get a cross thread exception.
Ray Booysen
UI interaction should be via events in the UI thread.
gimel
+6  A: 

As MrTelly said, the DispatcherTimer is the way to do this. It is tightly intergrated with the Dispatcher queue and makes sure that your callbacks are on the correct thread. Below are some good articles about this class. Some sample code is below detailing a basic example:

//  DispatcherTimer setup
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0,0,1);
dispatcherTimer.Start();

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    // Updating the Label which displays the current second
    lblSeconds.Content = DateTime.Now.Second;

    // Forcing the CommandManager to raise the RequerySuggested event
    CommandManager.InvalidateRequerySuggested();
}

Timer vs DispatcherTimer

MSDN Documentation

Ray Booysen
+1  A: 

The dispatcherTimer is the solution. I am designing an Audio/Video Player in WPF and was facing problem with using normal timer control. Thanks to Ray Booysen.

Regards Umesh