title: C# Timer to populate the database
body: <p>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).</p>
  
  <p>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.</p>
  
  <pre><code>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();
  }
  </code></pre>
  
  <p>This works, but...</p>
  
  <p>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?</p>
  
  <p>Any insight would help! Thank you in advance!</p>