Files
StockInfoCoreApp/Helpers/GlobalStopwatch.cs

101 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Helpers
{
public static class GlobalStopwatch
{
private static Stopwatch Stopwatch { get; set; }
static GlobalStopwatch()
{
Stopwatch = new Stopwatch();
}
public static void Start()
{
if (!Stopwatch.IsRunning)
{
_previousEllapsedMillis = 0;
Stopwatch.Start();
}
}
public static void Stop() => Stopwatch.Stop();
public static void Restart()
{
_previousEllapsedMillis = 0;
Stopwatch.Restart();
}
private static long _previousEllapsedMillis;
public static long EllapsedMillisPrevious
{
get
{
var ellapsed = EllapsedMillis;
var result = ellapsed - _previousEllapsedMillis;
_previousEllapsedMillis = ellapsed;
return result;
}
}
public static long EllapsedMillis => Stopwatch.ElapsedMilliseconds;
public static bool IsRunning
{
get
{
return Stopwatch.IsRunning;
}
}
public static void Print(string label)
{
if (label != null)
{
Debug.WriteLine($"{label}: {EllapsedMillis}");
}
else
{
Debug.WriteLine(EllapsedMillis);
}
}
public static void PrintWithLastTime(string label)
{
if (label != null)
{
Debug.WriteLine($"{label}: {EllapsedMillisPrevious}");
}
}
public static void PrintSecs(string label)
{
var secs = Math.Round((decimal)EllapsedMillis / 1000, 2);
if (label != null)
{
Debug.WriteLine($"{label}: {secs}");
}
else
{
Debug.WriteLine(secs);
}
}
public static void PrintWithLastTimeSecs(string label)
{
var secs = Math.Round((decimal)EllapsedMillisPrevious / 1000, 2);
if (label != null)
{
Debug.WriteLine($"{label}: {secs}");
}
}
}
}