Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4#region Using directives5 6using System;7using System.Threading;8using Microsoft.Management.Infrastructure.Options;9 10#endregion11 12namespace Microsoft.Management.Infrastructure.CimCmdlets13{14    /// <summary>15    /// Base action class, implemented to write results to pipeline.16    /// </summary>17    internal abstract class CimBaseAction18    {19        /// <summary>20        /// Initializes a new instance of the <see cref="CimBaseAction"/> class.21        /// </summary>22        protected CimBaseAction()23        {24        }25 26        /// <summary>27        /// <para>28        /// Execute the write operation to given cmdlet object29        /// </para>30        /// </summary>31        /// <param name="cmdlet">32        /// cmdlet wrapper object, to which write result.33        /// <see cref="CmdletOperationBase"/> for details.34        /// </param>35        public virtual void Execute(CmdletOperationBase cmdlet)36        {37        }38 39        /// <summary>40        /// <para>41        /// <see cref="XOperationContextBase"/> object that related to current action.42        /// It may used by action, such as <see cref="CimWriteResultObject"/>,43        /// since later on action may require namespace, and proxy object to reuse44        /// <see cref="CimSession"/>, <see cref="CimOperationOptions"/> object.45        /// </para>46        /// </summary>47        protected XOperationContextBase Context { get; set; }48    }49 50    /// <summary>51    /// <para>52    /// Synchronous action class, implemented to write results to pipeline53    /// and block current thread until the action is completed.54    /// </para>55    /// </summary>56    internal class CimSyncAction : CimBaseAction, IDisposable57    {58        /// <summary>59        /// Initializes a new instance of the <see cref="CimSyncAction"/> class.60        /// </summary>61        public CimSyncAction()62        {63            this.completeEvent = new ManualResetEventSlim(false);64            this.responseType = CimResponseType.None;65        }66 67        /// <summary>68        /// <para>69        /// Block current thread until action completed70        /// </para>71        /// </summary>72        /// <returns>Response from user.</returns>73        public virtual CimResponseType GetResponse()74        {75            this.Block();76            return responseType;77        }78 79        /// <summary>80        /// <para>81        /// Set the response result.82        /// </para>83        /// </summary>84        internal CimResponseType ResponseType85        {86            set { this.responseType = value; }87        }88 89        /// <summary>90        /// <para>91        /// Call this method when the action is completed or92        /// the operation is terminated.93        /// </para>94        /// </summary>95        internal virtual void OnComplete()96        {97            this.completeEvent.Set();98        }99 100        /// <summary>101        /// <para>102        /// Block current thread.103        /// </para>104        /// </summary>105        protected virtual void Block()106        {107            this.completeEvent.Wait();108            this.completeEvent.Dispose();109        }110 111        #region members112 113        /// <summary>114        /// Action completed event.115        /// </summary>116        private readonly ManualResetEventSlim completeEvent;117 118        /// <summary>119        /// Response result.120        /// </summary>121        protected CimResponseType responseType;122 123        #endregion124 125        #region IDisposable interface126        /// <summary>127        /// IDisposable interface.128        /// </summary>129        private bool _disposed;130 131        /// <summary>132        /// <para>133        /// Dispose() calls Dispose(true).134        /// Implement IDisposable. Do not make this method virtual.135        /// A derived class should not be able to override this method.136        /// </para>137        /// </summary>138        public void Dispose()139        {140            Dispose(true);141            // This object will be cleaned up by the Dispose method.142            // Therefore, you should call GC.SuppressFinalize to143            // take this object off the finalization queue144            // and prevent finalization code for this object145            // from executing a second time.146            GC.SuppressFinalize(this);147        }148 149        /// <summary>150        /// <para>151        /// Dispose(bool disposing) executes in two distinct scenarios.152        /// If disposing equals true, the method has been called directly153        /// or indirectly by a user's code. Managed and unmanaged resources154        /// can be disposed.155        /// If disposing equals false, the method has been called by the156        /// runtime from inside the finalizer and you should not reference157        /// other objects. Only unmanaged resources can be disposed.158        /// </para>159        /// </summary>160        /// <param name="disposing">Whether it is directly called.</param>161        protected virtual void Dispose(bool disposing)162        {163            // Check to see if Dispose has already been called.164            if (!this._disposed)165            {166                // If disposing equals true, dispose all managed167                // and unmanaged resources.168                if (disposing)169                {170                    // Dispose managed resources.171                    this.completeEvent?.Dispose();172                }173 174                // Call the appropriate methods to clean up175                // unmanaged resources here.176                // If disposing is false,177                // only the following code is executed.178 179                // Note disposing has been done.180                _disposed = true;181            }182        }183        #endregion184    }185}186