// // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. // using System; using System.Diagnostics; using System.Threading; using System.Threading.Tasks; using Microsoft.SqlTools.Utility; namespace Microsoft.SqlTools.ServiceLayer.Scripting { /// /// Base class for scripting operations. Because scripting operations can be very long /// running, there my be multiple concurrent scripting operations. To distinguish events /// between concurrent scripting operations, use the operation id. /// public abstract class ScriptingOperation : IDisposable { private CancellationTokenSource cancellation = new CancellationTokenSource(); protected ScriptingOperation() { this.OperationId = Guid.NewGuid().ToString(); } protected CancellationToken CancellationToken { get { return this.cancellation.Token; } } /// /// Gets the unique id associated with this instance. /// public string OperationId { get; private set; } /// /// Excecutes the scripting operation. /// public abstract void Execute(); /// /// Cancels the scripting operation. /// public virtual void Cancel() { if (!this.cancellation.IsCancellationRequested) { Logger.Write(TraceEventType.Verbose, string.Format("Cancel invoked for OperationId {0}", this.OperationId)); this.cancellation.Cancel(); } } /// /// Disposes the scripting operation. /// public abstract void Dispose(); } }