private void InitializeTimer(double timeInterval) { if (timeInterval > 0) { ServiceTimer.Dispose(); ServiceTimer = new Timer(); ServiceTimer.Enabled = true; ServiceTimer.Elapsed += new ElapsedEventHandler(timer1_Tick); ServiceTimer.Interval = 10000; } }
/// <summary> /// The method that will be executed when the service is stopped. This method /// disposes of the <see cref="ServiceTimer"/>. The <see cref="OnStop"/> /// abstract method will also be called if the service requires additional functionality /// on stopping. This method will catch and log any exceptions that occur on stopping. /// </summary> public void Stop() { try { Log.Info("Stopping periodic service."); if (ServiceTimer != null) { ServiceTimer.Dispose(); } OnStop(); } catch (Exception ex) { Log.ErrorFormat("An error occured while stopping the service: \n{0}", ex); } }
protected override void OnStop() { ServiceTimer.Enabled = false; ServiceTimer.Dispose(); ServiceTimer = null; }