private void DoBackgroundJob(ICancellationProvider cancellation) { Debug.WriteLine("Do something"); // if canceled, stop immediately cancellation.CheckForCancelAndBreak(); Debug.WriteLine("Do something more"); if (cancellation.CheckForCancel()) { // you noticed cancellation but still need to finish something Debug.WriteLine("Do some necessary clean up"); return; } // Sleep but cancel will stop and break cancellation.SleepWithCancel(10000); Debug.WriteLine("Last bit of work"); }
/// <summary> /// Instantiates the disposable. /// </summary> /// <param name="provider">The cancellation provider.</param> /// <param name="identifier">The GUID of the operation to cancel upon disposal.</param> public RemoteCancellationDisposable(ICancellationProvider provider, Guid identifier) { _provider = provider ?? throw new ArgumentNullException(nameof(provider)); _guid = identifier; }