title: Xamarin Timer Being Created Multiple Times
body: <p>I am struggling with my Xamarin Timer implementation and I was hoping someone could explain some of the behaviour I am seeing.</p>
  
  <p>My application has a background service which is running and start from a Broadcast receiver ActionBootComplete.  Relevant code below.</p>
  
  <pre><code>{Android.App.Application.Context.StartService( new Intent( Android.App.Application.Context, typeof( UDPService ) ) ); }
  
  {Android.App.Application.Context.BindService( udpServiceIntent, udpServiceConnection, Bind.AutoCreate );}
  </code></pre>
  
  <p>On My Main Activity I have a timer which is essentially a Health check timer that send an online message to our service.  The timer interval is customisable with the default value being event 15 seconds.</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Health Check Timer
      /// &lt;/summary&gt;
      System.Timers.Timer m_healthCheckTimer;
  </code></pre>
  
  <p>In the OnCreate Method I initialise and start the timer as per the below :-</p>
  
  <pre><code>        m_healthCheckTimer = new System.Timers.Timer(m_healthPingInterval * 1000);
  
          m_healthCheckTimer.Elapsed += new System.Timers.ElapsedEventHandler(m_healthCheck_Elapsed);
  
          m_healthCheckTimer.Start();
  </code></pre>
  
  <p>When the Timer elapses I fire the following code with some just being for debugging :-</p>
  
  <pre><code> void m_healthCheck_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
  
          try {
  
              LogFile.WriteInputLog(string.Format("Health Check Time Elapsed - Count  {0}", m_healthCounter), "Manager");
  
              m_healthCounter++;
  
              var nextValidPingString = string.Format("{0:hh:mm:ss}", DateTime.Now.AddSeconds( m_healthPingInterval ) ) ;
  
              HealthCheckin(string.Format("{0} - Next Send Time {1}", "Main Activity Update Timer", nextValidPingString));
  
          } catch { }
  
      }
  </code></pre>
  
  <p>Essentially what I am seeing is the timer works perfectly for a time period, could be hours, could be days then I notice that the timer fires multiple times.  Inside the Log Files I will see 2+ counter instances incrementing seperately.  So Health Check 100 / 101 / 102 and mixed in with the logging may be Health Check 200 / 201 / 202.</p>
  
  <p>My assumption is when the app is going into the background and then foreground, my Main Activity is being recreated which is creating a second instance of the timer.</p>
  
  <p>I think the best way forward is to move the Timer to the Background service but for my own knowledge, I would like to know in my current scenario what could be done.</p>
  
  <p>I cant seem to find an event that I can override that tells me the my Main Activity is being "recreated" so I could stop the original Timer or a method to override when the Main Activity is being disposed of to do the same sort of cleanup.</p>
  
  <p>Thanks in advance,</p>
  
  <p>Daniel.</p>