Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Threading;
- namespace Common.Services
- {
- //Example:
- /*
- public class YourTimerService : TimerServiceBase
- {
- protected override void OnTimerInterval(object state)
- {
- Console.WriteLine("OnTimerInterval called!");
- }
- }
- */
- /// <summary>
- /// Base class that uses a timer to execute functionality
- /// </summary>
- public abstract class TimerServiceBase : IDisposable
- {
- readonly Timer _pollTimer;
- public TimeSpan Interval { get; protected set; }
- public bool IsStarted { get; protected set; }
- public TimerServiceBase(object state = null)
- : this(5000, state) { }
- public TimerServiceBase(int intervalMilliseconds, object state = null)
- : this(TimeSpan.FromMilliseconds(intervalMilliseconds), state) { }
- public TimerServiceBase(TimeSpan interval, object state = null)
- {
- this.Interval = interval;
- _pollTimer = new Timer(OnTimerInterval, state, Timeout.Infinite, Timeout.Infinite);
- }
- /// <summary>
- /// Called when the underlying timer is executed
- /// </summary>
- /// <param name="state">Optional state passed in via the ctor</param>
- protected abstract void OnTimerInterval(object state);
- /// <summary>
- /// Starts the underlying timer
- /// </summary>
- /// <param name="args">Optional arguments</param>
- public void Start(params string[] args)
- {
- Start(TimeSpan.FromSeconds(0), args);
- }
- /// <summary>
- /// Starts the underlying timer
- /// </summary>
- /// <param name="delay">A <see cref="T:System.TimeSpan"/> representing the amount of time to delay before invoking the OnTimerInterval method</param>
- /// <param name="args">Optional arguments</param>
- public void Start(TimeSpan dueTime, params string[] args)
- {
- if (IsStarted)
- throw new InvalidOperationException("Service already started!");
- if (!OnStart(args))
- return;
- IsStarted = true;
- _pollTimer.Change(dueTime, Interval);
- }
- /// <summary>
- /// Called when the Start() method is called. Return true to continue to start the service otherwise false
- /// </summary>
- /// <param name="args">Optional arguments passed in from the Start() method</param>
- /// <returns>True to continue to start the service otherwise false</returns>
- protected virtual bool OnStart(string[] args)
- {
- return true;
- }
- /// <summary>
- /// Stops the underlying timer from calling the OnTimerInterval method
- /// </summary>
- public void Stop()
- {
- if (!IsStarted)
- throw new InvalidOperationException("Service already stopped!");
- if (!OnStop())
- return;
- IsStarted = false;
- _pollTimer.Change(Timeout.Infinite, Timeout.Infinite);
- }
- /// <summary>
- /// Called when the Stop() method is called. Return true to continue to stop the service otherwise false to cancel the stopping operation
- /// </summary>
- /// <returns>True to continue to stop the service otherwise false</returns>
- protected virtual bool OnStop()
- {
- return true;
- }
- #region IDisposable Pattern
- protected bool IsDisposed { get; private set; }
- [System.Diagnostics.DebuggerStepThrough]
- protected void EnsureNotDisposed()
- {
- if (IsDisposed)
- throw new ObjectDisposedException(GetType().Name);
- }
- [System.Diagnostics.DebuggerStepThrough]
- public virtual void Dispose()
- {
- if (IsDisposed)
- throw new ObjectDisposedException(this.GetType().Name);
- try
- {
- this.Dispose(true);
- }
- finally
- {
- GC.SuppressFinalize(this);
- }
- }
- [System.Diagnostics.DebuggerStepThrough]
- protected virtual void Dispose(bool disposing)
- {
- try
- {
- if (disposing && !IsDisposed)
- {
- //Do object cleanup here
- _pollTimer.Change(Timeout.Infinite, Timeout.Infinite);
- _pollTimer.Dispose();
- }
- }
- finally
- {
- this.IsDisposed = true;
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment