Files
1257B_DB/JinYuan.Helper/TimedOperation.cs
T
2026-08-06 09:23:30 +08:00

37 lines
997 B
C#

using System;
using System.Diagnostics;
using System.Threading;
namespace JinYuan.Helper
{
/// <summary>
/// 操作计时功能
/// </summary>
public class TimedOperation : IDisposable
{
private readonly Stopwatch _stopwatch;
private readonly string _operationName;
private readonly Action<long, string> _onCompleted;
private bool _disposed = false;
public TimedOperation(string operationName, Action<long, string> onCompleted = null)
{
_stopwatch = Stopwatch.StartNew();
_operationName = operationName;
_onCompleted = onCompleted;
}
public long ElapsedMilliseconds => _stopwatch.ElapsedMilliseconds;
public void Dispose()
{
if (!_disposed)
{
_stopwatch.Stop();
_onCompleted?.Invoke(_stopwatch.ElapsedMilliseconds, _operationName);
_disposed = true;
}
}
}
}