Reputation: 646
I have a little problem. I didn't find a suitable title, sorry
I have a time column at my database and I want to do something (not important) whenever currenttime==timeValueAtMyDatabase
. How?
if(currenttime==timeValueAtMyDatabase)
{
//do something
}
Upvotes: 0
Views: 151
Reputation: 7432
If you are okay with the code executing in a different thread and you can use whatever libraries you want (Rx in this case), I personally prefer this method:
Observable.Timer(timeToDoTheThing).Subscribe(codeToRun);
timeToDoTheThing
is the time that the code should be executed at and codeToRun
is the code that will run when the time is reached (in the form of a void returning delegate that takes one long parameter).
Some things to note:
Below is a complete program that will exemplify this. You will need to install the Rx-Main NuGet package to the project for it to work and build against .NET 4+.
using System;
using System.Reactive.Linq;
using System.Threading;
namespace Test
{
public class Class
{
public static void Main()
{
var timeToDoTheThing = new DateTimeOffset(2013, 10, 19, 22, 19, 30, TimeSpan.Zero);
Action<long> codeToRun = _ => Console.WriteLine("It is time.");
Observable.Timer(timeToDoTheThing).Subscribe(codeToRun);
Thread.Sleep(100000);
}
}
}
Upvotes: 2
Reputation: 4408
Use a timer control that ticks on appropriate intervals. On ever tick compare the current time with the time in your database. You will need to account for differences. For example if it ticks at 0.1sec, 1.1sec, 2.1sec and the time in your database is 1sec. You'll have to find an appropriate way to account for such differences.
Upvotes: 1
Reputation: 5525
If this is a Windows Forms Application, you can use a timer and check whether some of the entries are less then DateTime.Now
.
If it is a web application, you can use a cron job. Check out the following link: Creating Cron Jobs in C#.
Upvotes: 1