/// <summary> /// Break Merging Operation manually /// This method is supported by version 18.7 or greater /// </summary> public static void BreakMergingManually() { // Creating cancellation object Cancellation cancellation = new Cancellation(); // cancelation after 5 seconds cancellation.CancelAfter(5000); // Load index Index index = new Index(Utilities.indexPath); index.Merge(cancellation); }
public CancellableOperation(TimeSpan?timeout, CancellationToken cancellationToken) { Timeout = timeout; Completion = new TaskCompletionSource <TResult>(TaskCreationOptions.RunContinuationsAsynchronously); Completion.Task.ContinueWith( (task, state) => { var operation = (CancellableOperation <TResult>)state !; operation.Dispose(); }, this, cancellationToken, TaskContinuationOptions.ExecuteSynchronously, // We need to execute synchronously to clean-up before anything else continues TaskScheduler.Default); Cancellation = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); if (Timeout != null && Timeout != System.Threading.Timeout.InfiniteTimeSpan && Timeout != TimeSpan.MaxValue) { Cancellation.CancelAfter(Timeout.Value); } CancellationRegistration = Cancellation.Token.Register( (self) => { var operation = (CancellableOperation <TResult>)self !; if (cancellationToken.IsCancellationRequested) { // The operation was externally canceled before it timed out. Dispose(); return; } operation.Completion.TrySetException(new TimeoutException($"The operation timed out after {Timeout}.")); operation.Cancellation?.Dispose(); operation.CancellationRegistration.Dispose(); }, this); }