/* Classe MySimpleWatch Mesure facile avec StopWatch Version 1.0 - Last Modif : 14/02/2008 14:30 Développé par Sebastien WARIN < sebastien [arobase] warin [point] fr > (c) 2008 - http://sebastien.warin.fr */ using System; using System.Diagnostics; public class MySimpleWatch { /* Private field */ private Stopwatch stopWatch; /* Constructor*/ public MySimpleWatch() { stopWatch = new Stopwatch(); } /* Public methods */ public void DisplayWatch(Action action) { DisplayWatch(String.Empty, action); } public void DisplayWatch(string name, Action action) { Console.WriteLine("------------ Start {0} ------------", name); Watch(action, (t => Console.WriteLine("------------ Stop ------------\n" + "ElapsedTime : {0} ms", t.ElapsedMilliseconds.ToString()))); } public void ProcessWatch(Action action, Action result) { Watch(action, result); } public Stopwatch SimpleWatch(Action action) { return Watch(action, null); } /* Private method */ private Stopwatch Watch(Action action, Action result) { if (stopWatch == null) stopWatch = new Stopwatch(); else stopWatch.Reset(); // Process the action to mesurate stopWatch.Start(); action(); stopWatch.Stop(); // Process the result ? if (result != null) result(stopWatch); // Return the Stopwatch return stopWatch; } }