Multiple Stop Watches

class SysTimers { private Dictionary<string,Stopwatch> _watches; public SysTimers() { } public void Start(string watch){ if(_watches.Keys.Contains(watch)) { _watches[watch].Reset(); _watches[watch].Start(); } else { _watches.Add(watch, new Stopwatch()); _watches[watch].Start(); } } public void Stop(string watch) { if (_watches.Keys.Contains(watch)) { if (_watches[watch].IsRunning) { _watches[watch].Stop(); } } } public TimeSpan GetTime(string watch) { if (_watches.Keys.Contains(watch)) { if (_watches[watch].IsRunning) { _watches[watch].Stop(); } return _watches[watch].Elapsed; } else { _watches.Add(watch, new Stopwatch()); return _watches[watch].Elapsed; } } }
For nice output stopwatches.GetTime(Name).ToString("mm\\:ss\\.ff");
SysTimers stopwatches = new SysTimers();
stopwatches.Start(Name);

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.