public void DisposableActionCalledOnDispose() { bool disposableCalled = false; using (DisposableAction action = new DisposableAction(delegate { disposableCalled = true; })) {} Assert.IsTrue(disposableCalled); }
public void Dispose_ActionIsCalled() { var count = 0; Action action = () => ++count; var disposable = new DisposableAction(action); disposable.Dispose(); Assert.AreEqual(1, count); }
public void DisposableActionGetsCorrectParameterFromCtor() { int expected = 4543; int actual = 0; DisposableAction<int> action = new DisposableAction<int>(delegate(int i) { actual = i; }, expected); action.Dispose(); Assert.AreEqual(expected, actual); }
public static IDisposable LogTimed(this ILogger self, string activity) { if (self.IsEnabled(LogLevel.Debug)) { self.LogDebug("Started " + activity); Stopwatch sw = new Stopwatch(); var ret = new DisposableAction(() => { sw.Stop(); self.LogDebug($"Finished {activity} in {sw.ElapsedMilliseconds:0.00}ms"); }); sw.Start(); return ret; } else { return DisposableAction.Null; } }