using System; using System.Diagnostics; using System.Threading; namespace JinYuan.Helper { /// /// 操作计时功能 /// public class TimedOperation : IDisposable { private readonly Stopwatch _stopwatch; private readonly string _operationName; private readonly Action _onCompleted; private bool _disposed = false; public TimedOperation(string operationName, Action 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; } } } }