Line | |
---|
1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Diagnostics;
|
---|
6 |
|
---|
7 | namespace 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.