You can setup a timer in the Global.asax.cs
file that'll go off every X hours, or you could also create a scheduled tasks that goes off every X hours as well to trigger the clean up service.
If you do not have a Global file in the project you can simply add one by adding one to the project. To do this right-click on the project -> Add -> New Item, and then in the dialog that pops up in select Global Application Class and hit Add. Then inside the Application_Start
event you can initialize your timer to perform the action.
public class Global : System.Web.HttpApplication
{
private static System.Threading.Timer timer;
protected void Application_Start(object sender, EventArgs e)
{
var howLongTillTimerFirstGoesInMilliseconds = 1000;
var intervalBetweenTimerEventsInMilliseconds = 2000;
Global.timer = new Timer(
(s) => SomeFunc(),
null, // if you need to provide state to the function specify it here
howLongTillTimerFirstGoesInMilliseconds,
intervalBetweenTimerEventsInMilliseconds
);
}
private void SomeFunc()
{
// reoccurring task code
}
protected void Application_End(object sender, EventArgs e)
{
if(Global.timer != null)
Global.timer.Dispose();
}
}
For more information on the Global file you may want to refer to MSDN