mirror of
https://mirror.ghproxy.com/https://github.com/dexyfex/CodeWalker
synced 2024-09-29 04:27:30 +08:00
47 lines
1.2 KiB
C#
47 lines
1.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Text;
|
|
|
|
namespace CodeWalker.Core.Utils
|
|
{
|
|
public class DisposableTimer : IDisposable
|
|
{
|
|
public static event Action<TimeSpan, string> TimerStopped;
|
|
public Stopwatch Stopwatch { get; init; }
|
|
|
|
static DisposableTimer()
|
|
{
|
|
#if DEBUG
|
|
TimerStopped += (timeSpan, name) => Debug.WriteLine($"{name} took {timeSpan.TotalMilliseconds} ms");
|
|
#endif
|
|
TimerStopped += (timeSpan, name) => Console.WriteLine($"{name} took {timeSpan.TotalMilliseconds} ms");
|
|
}
|
|
|
|
public string Name { get; private set; }
|
|
|
|
public DisposableTimer(string name)
|
|
{
|
|
Stopwatch = Stopwatch.StartNew();
|
|
Name = name;
|
|
}
|
|
public DisposableTimer(string name, bool start)
|
|
{
|
|
Name = name;
|
|
if (start)
|
|
{
|
|
Stopwatch = Stopwatch.StartNew();
|
|
} else
|
|
{
|
|
Stopwatch = new Stopwatch();
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Stopwatch.Stop();
|
|
TimerStopped?.Invoke(Stopwatch.Elapsed, Name ?? string.Empty);
|
|
}
|
|
}
|
|
}
|