I am using global.asax to send mail at regular interval. The time to send mail is set in db and can be any time in 24 hours(like 1 am, 1 pm, 6 am and so on). This is my code :
void Application_Start(object sender, EventArgs e)
{
System.Threading.Timer _timer = new System.Threading.Timer(new System.Threading.TimerCallback(GetSchedule));
_timer.Change(0, (1000 * 60 * 60));
}
void GetSchedule(Object state)
{
try
{
lock (state)
{
EmailingReportToCustomers.GetDBAndReportTypeOfCustomers();
}
}
catch (Exception ex)
{
}
}
public static void GetDBAndReportTypeOfCustomers()
{
//Queries db for time in table. If time now is db time. Then SendMail()
}
The mail is send at given time as needed. The problem I am facing here is same mail is being sent multiple times (8 times). I think GetDBAndReportTypeOfCustomers() is being queried multiple times.
What I have tried:
Initially there was no _timer.Change(0, (1000 * 60 * 60));. After some research, I added timer as said my them to control multiple sending of mail. But it is not working, Any help ???