2

Is it possible to automate a method to run every x minutes/hours within a web application (server side of course)?

Simplified code:

public class Task
{
    public string Name { get; set; }
    public DateTime Deadline { get; set; }
    ...
}

The case: Each task has a deadline, I want to generate a mail if the deadline is approaching.

There are some answers, e.g. here with System.Threading or embed the functionality into a constructor. But how can I make sure, it will work for months without to be called?

Thank you!

Community
  • 1
  • 1
Ivano
  • 45
  • 6

2 Answers2

2

My favorite tool for this is Hangfire. It lets you schedule tasks, your task would look like this:

RecurringJob.AddOrUpdate(() => CheckIfDeadlineIsApproaching(), Cron.Hourly);

Unlike the suggestions in the comments this doesn't require a Windows Service either, which makes this solution much easier.

Leon Cullens
  • 12,276
  • 10
  • 51
  • 85
0

You should put scheduler in application start. I think Global.asax.cs file

Ebrahim Pasbani
  • 9,168
  • 2
  • 23
  • 30
  • Who vote me down, you can look at any lib dos to make sure to run any persistent process, it should be placed in application start in web app. – Ebrahim Pasbani Jul 08 '16 at 12:15