I am trying to retrieve the data from MSSQL and PostrgeSQL databases to populate it into a MySQL database on a regular basis (every hour).
Based on a research I’ve done, I would like to use a C# Timer to do that. However, I do not fully understand the way timer works.
I declare it in the Page_Load, set the elapsed event handler and call a method from the event handler.
protected void Page_Load(object sender, EventArgs e)
{
int duration = 100 * 60 * 5; //milliseconds * seconds * minutes
// Create a timer with a two second interval.
System.Timers.Timer timer = new System.Timers.Timer(duration);
// Hook up the Elapsed event for the timer.
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
cf();
}
This works, but…
The problem is that this way the timer is being initialized every time the page is loaded. How do I create a timer which is initialized only once and keeps running from that moment without being reinitialized. Also, how do I handle the situation if the timer or a method fails – throws an exception, or a server goes down? How do I restart the timer then?
Any insight would help! Thank you in advance!
You do not want to do this on a ASP.NET Web Page. The ASP.NET timer is designed for actions to occurs during the lifetime of a page before you navigate away.
IF you want to do long running work like this (i.e. a task once-per-hour 24/7) you would be much better off either:
Another option, since you’re task seems to be heavily related to database data transfer would be to use something like SSIS or another alternative ETL Tool (Extract-Transform-Load)