public void OnNextFlush(AsyncFlushHandler handler) { var myAction = new MyAction(handler); if (Interlocked.CompareExchange(ref flushAction, myAction, null) != null) { throw new InvalidOperationException(); } }
async Task FlushAsync(string message, AsyncFlushFunc func, AsyncFlushHandler handler, CancellationToken cancellationToken) { LogDebug(message); try { await handler(func, cancellationToken).ConfigureAwait(false); LogDebug($"{message} done"); } catch (Exception ex) { if (IgnoreErrors) { return; } LogDebug($"{message} failed: {ex}"); throw; } }
public override Task FlushAsync(CancellationToken cancellationToken) { var message = $"{Name}.FlushAsync()"; AsyncFlushFunc asyncBaseFlush = base.FlushAsync; AsyncFlushHandler asyncFlushHandler = (func, ct) => func(ct); var action = Interlocked.Exchange(ref flushAction, null); if (action?.AsyncFlush != null) { message += " - action"; return(FlushAsync(message, asyncBaseFlush, action.AsyncFlush, cancellationToken)); } return(FlushAsync(message, asyncBaseFlush, asyncFlushHandler, cancellationToken)); }
public MyAction(AsyncFlushHandler handler) { AsyncFlush = handler; }