andrew4582

Stopwatch with Callback delegates

Aug 15th, 2010
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace System {
  7.    
  8.     /// <summary>
  9.     /// General stop watch with callback delegates
  10.     /// </summary>
  11.     public class StopWatch:IDisposable {
  12.         public const string NUMBER_FORMAT = "#,#";
  13.         //StopWatch.GlobalDurationCallBack += new Action<TimeSpan,string>((duration,name) => {
  14.         //  //do something
  15.         //});
  16.         public static Action<TimeSpan,string> GlobalDurationCallBack { get; set; }
  17.        
  18.         #region Static Create()
  19.         public static StopWatch Create() {
  20.             return new StopWatch(GlobalDurationCallBack);
  21.         }
  22.         public static StopWatch Create(string name) {
  23.             return new StopWatch(GlobalDurationCallBack) { Name = name };
  24.         }
  25.         public static StopWatch Create(string name,Action<TimeSpan,string> durationCallBack) {
  26.             return new StopWatch(name,durationCallBack);
  27.         }
  28.         public static StopWatch Create(Action<TimeSpan,string> durationCallBack) {
  29.             return new StopWatch(durationCallBack);
  30.         }
  31.         #endregion
  32.  
  33.         long _start;
  34.         TimeSpan _duration;
  35.         Action<TimeSpan,string> _durationCallBack;
  36.         string _name;
  37.  
  38.         public string Name {
  39.             get { return _name; }
  40.             set { _name = value; }
  41.         }
  42.         public TimeSpan Duration {
  43.             get { return _duration; }
  44.         }
  45.  
  46.         #region ctr
  47.         public StopWatch()
  48.             : this(null,GlobalDurationCallBack,true) {
  49.         }
  50.         public StopWatch(string name)
  51.             : this(name,GlobalDurationCallBack,true) {
  52.         }
  53.         public StopWatch(string name,Action<TimeSpan,string> durationCallBack)
  54.             : this(name,durationCallBack,true) {
  55.         }
  56.         public StopWatch(Action<TimeSpan,string> durationCallBack)
  57.             : this(null,durationCallBack,true) {
  58.         }
  59.         public StopWatch(Action<TimeSpan,string> durationCallBack,bool startStopWatch)
  60.             : this(null,durationCallBack,startStopWatch) {
  61.         }
  62.         public StopWatch(bool startStopWatch)
  63.             : this(null,GlobalDurationCallBack,startStopWatch) {
  64.         }
  65.         public StopWatch(string name,Action<TimeSpan,string> durationCallBack,bool startStopWatch) {
  66.             _name = name;
  67.             _durationCallBack = durationCallBack;
  68.  
  69.             Reset();
  70.             if(startStopWatch)
  71.                 Start();
  72.         }
  73.         #endregion
  74.  
  75.         public void Reset() {
  76.             _duration = TimeSpan.Zero;
  77.             _start = 0;
  78.         }
  79.         public void Start() {
  80.             Reset();
  81.             _start = DateTime.Now.Ticks;
  82.         }
  83.         public TimeSpan Stop() {
  84.             if(_duration == TimeSpan.Zero) {
  85.                 long end = DateTime.Now.Ticks;
  86.                 _duration = new TimeSpan(end - _start);
  87.             }
  88.             if(_durationCallBack != null)
  89.                 _durationCallBack(_duration,_name);
  90.             return _duration;
  91.         }
  92.         public override string ToString() {
  93.             return ToString(NUMBER_FORMAT);
  94.         }
  95.         public string ToString(string millisecondsFormat) {
  96.             double totalMilliseconds = -1.0D;
  97.             if(Duration != TimeSpan.Zero)
  98.                 totalMilliseconds = Duration.TotalMilliseconds;
  99.             return totalMilliseconds.ToString(millisecondsFormat);
  100.         }
  101.         public void Dispose() {
  102.             Stop();
  103.         }
  104.     }
  105. }
Add Comment
Please, Sign In to add comment