using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Threading = System.Threading;
using System.Diagnostics;
namespace Yaulw.Other
{
///
/// Wrapper Class arround Tracing, designed to measure performance.
/// Use this class to measure performance arround your calls.
///
/// TraceM.TraceBegin()
/// ... Some Code....
/// ... Some More Code...
/// TraceM.TraceEnd("Custom Message");
///
///
public static class TraceM
{
#region Private Statics
private static Dictionary> s_ThreadTraceBeginTSMap = new Dictionary>();
public static bool EnableTracing { get; set; }
#endregion
#region Construction
///
/// Construction * Tracing by default is disabled *
///
static TraceM()
{
EnableTracing = false;
}
#endregion
#region Public Statics
///
/// Call this to Start the Performance Trace
///
public static void TraceBegin()
{
if (EnableTracing)
{
uint curThreadId = (uint)Threading.Thread.CurrentThread.ManagedThreadId;
if (!s_ThreadTraceBeginTSMap.ContainsKey(curThreadId))
s_ThreadTraceBeginTSMap[curThreadId] = new Stack();
s_ThreadTraceBeginTSMap[curThreadId].Push(DateTime.Now);
}
}
///
/// Us this to End the Performance Trace
///
/// Custom Message you want displayed in the Trace Window
public static void TraceEnd(string CustomMessage = "")
{
if (EnableTracing)
{
uint curThreadId = (uint)Threading.Thread.CurrentThread.ManagedThreadId;
if (s_ThreadTraceBeginTSMap.ContainsKey(curThreadId))
{
DateTime orgTime = s_ThreadTraceBeginTSMap[curThreadId].Pop();
TimeSpan ts = DateTime.Now - orgTime;
Trace.WriteLine((CustomMessage + " (Time Taken in ms " + ts.TotalMilliseconds.ToString() + ")"));
}
}
}
#endregion
}
}