Check if x months have passed in asp.net mvc
3 answers
Steve's answers above make a simple example of this. If you want to do this automatically, you have to put it in some services and call it from the scheduler or some services like Hangfire or Quartz .
(DateTime.Now - StartDate)
If this check has something to do with sending email notification, etc., I prefer to check it from the database and run a SQL job to run it every day, or anytime you think it fits.
+3
source to share
You can use it asynchronously using a timer on the server. This would mean that the service must remain for x months. The best choice would most likely be to store the end date and start a timer to call the function when that date condition is true.
//Timer set to run every 20 seconds
Timer = new System.Timers.Timer(20000);
//Timer set to run every 20 seconds
Timer.Elapsed += SendTimerElapsed;
Timer.Start();
Here's an example:
class Program
{
static void Main(string[] args)
{
Timer t = new System.Timers.Timer(1000);
t.Elapsed += Hitme;
t.Start();
Console.ReadLine();
}
private static void Hitme(object sender, ElapsedEventArgs e)
{
Console.WriteLine("Ouch");
}
}
0
source to share