// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. #region Using directives using System; using System.Threading; using Microsoft.Management.Infrastructure.Options; #endregion namespace Microsoft.Management.Infrastructure.CimCmdlets { /// /// Base action class, implemented to write results to pipeline. /// internal abstract class CimBaseAction { /// /// Initializes a new instance of the class. /// protected CimBaseAction() { } /// /// /// Execute the write operation to given cmdlet object /// /// /// /// cmdlet wrapper object, to which write result. /// for details. /// public virtual void Execute(CmdletOperationBase cmdlet) { } /// /// /// object that related to current action. /// It may used by action, such as , /// since later on action may require namespace, and proxy object to reuse /// , object. /// /// protected XOperationContextBase Context { get; set; } } /// /// /// Synchronous action class, implemented to write results to pipeline /// and block current thread until the action is completed. /// /// internal class CimSyncAction : CimBaseAction, IDisposable { /// /// Initializes a new instance of the class. /// public CimSyncAction() { this.completeEvent = new ManualResetEventSlim(false); this.responseType = CimResponseType.None; } /// /// /// Block current thread until action completed /// /// /// Response from user. public virtual CimResponseType GetResponse() { this.Block(); return responseType; } /// /// /// Set the response result. /// /// internal CimResponseType ResponseType { set { this.responseType = value; } } /// /// /// Call this method when the action is completed or /// the operation is terminated. /// /// internal virtual void OnComplete() { this.completeEvent.Set(); } /// /// /// Block current thread. /// /// protected virtual void Block() { this.completeEvent.Wait(); this.completeEvent.Dispose(); } #region members /// /// Action completed event. /// private readonly ManualResetEventSlim completeEvent; /// /// Response result. /// protected CimResponseType responseType; #endregion #region IDisposable interface /// /// IDisposable interface. /// private bool _disposed; /// /// /// Dispose() calls Dispose(true). /// Implement IDisposable. Do not make this method virtual. /// A derived class should not be able to override this method. /// /// public void Dispose() { Dispose(true); // This object will be cleaned up by the Dispose method. // Therefore, you should call GC.SuppressFinalize to // take this object off the finalization queue // and prevent finalization code for this object // from executing a second time. GC.SuppressFinalize(this); } /// /// /// Dispose(bool disposing) executes in two distinct scenarios. /// If disposing equals true, the method has been called directly /// or indirectly by a user's code. Managed and unmanaged resources /// can be disposed. /// If disposing equals false, the method has been called by the /// runtime from inside the finalizer and you should not reference /// other objects. Only unmanaged resources can be disposed. /// /// /// Whether it is directly called. protected virtual void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!this._disposed) { // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { // Dispose managed resources. this.completeEvent?.Dispose(); } // Call the appropriate methods to clean up // unmanaged resources here. // If disposing is false, // only the following code is executed. // Note disposing has been done. _disposed = true; } } #endregion } }