Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Common/Auxiliary/DisposableTimer.cs @ 13792

Last change on this file since 13792 was 12503, checked in by aballeit, 9 years ago

#2283 added GUI and charts; fixed MCTS

File size: 927 bytes
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Diagnostics;
6
7namespace Microsoft.Research.DynamicDataDisplay.Common.Auxiliary
8{
9  public sealed class DisposableTimer : IDisposable
10  {
11    private bool isActive = true;
12    private readonly string name;
13    Stopwatch timer;
14    public DisposableTimer(string name) : this(name, true) { }
15
16    public DisposableTimer(string name, bool isActive)
17    {
18      this.name = name;
19      this.isActive = isActive;
20      if (isActive)
21      {
22        timer = Stopwatch.StartNew();
23        Trace.WriteLine(name + ": started " + DateTime.Now.TimeOfDay);
24      }
25    }
26
27    #region IDisposable Members
28
29    public void Dispose()
30    {
31      //#if DEBUG
32      if (isActive)
33      {
34        var duration = timer.ElapsedMilliseconds;
35        Trace.WriteLine(name + ": elapsed " + duration + " ms.");
36        timer.Stop();
37      }
38      //#endif
39    }
40
41    #endregion
42  }
43}
Note: See TracBrowser for help on using the repository browser.