File size: 5,727 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// 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
{
    /// <summary>
    /// Base action class, implemented to write results to pipeline.
    /// </summary>
    internal abstract class CimBaseAction
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CimBaseAction"/> class.
        /// </summary>
        protected CimBaseAction()
        {
        }

        /// <summary>
        /// <para>
        /// Execute the write operation to given cmdlet object
        /// </para>
        /// </summary>
        /// <param name="cmdlet">
        /// cmdlet wrapper object, to which write result.
        /// <see cref="CmdletOperationBase"/> for details.
        /// </param>
        public virtual void Execute(CmdletOperationBase cmdlet)
        {
        }

        /// <summary>
        /// <para>
        /// <see cref="XOperationContextBase"/> object that related to current action.
        /// It may used by action, such as <see cref="CimWriteResultObject"/>,
        /// since later on action may require namespace, and proxy object to reuse
        /// <see cref="CimSession"/>, <see cref="CimOperationOptions"/> object.
        /// </para>
        /// </summary>
        protected XOperationContextBase Context { get; set; }
    }

    /// <summary>
    /// <para>
    /// Synchronous action class, implemented to write results to pipeline
    /// and block current thread until the action is completed.
    /// </para>
    /// </summary>
    internal class CimSyncAction : CimBaseAction, IDisposable
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CimSyncAction"/> class.
        /// </summary>
        public CimSyncAction()
        {
            this.completeEvent = new ManualResetEventSlim(false);
            this.responseType = CimResponseType.None;
        }

        /// <summary>
        /// <para>
        /// Block current thread until action completed
        /// </para>
        /// </summary>
        /// <returns>Response from user.</returns>
        public virtual CimResponseType GetResponse()
        {
            this.Block();
            return responseType;
        }

        /// <summary>
        /// <para>
        /// Set the response result.
        /// </para>
        /// </summary>
        internal CimResponseType ResponseType
        {
            set { this.responseType = value; }
        }

        /// <summary>
        /// <para>
        /// Call this method when the action is completed or
        /// the operation is terminated.
        /// </para>
        /// </summary>
        internal virtual void OnComplete()
        {
            this.completeEvent.Set();
        }

        /// <summary>
        /// <para>
        /// Block current thread.
        /// </para>
        /// </summary>
        protected virtual void Block()
        {
            this.completeEvent.Wait();
            this.completeEvent.Dispose();
        }

        #region members

        /// <summary>
        /// Action completed event.
        /// </summary>
        private readonly ManualResetEventSlim completeEvent;

        /// <summary>
        /// Response result.
        /// </summary>
        protected CimResponseType responseType;

        #endregion

        #region IDisposable interface
        /// <summary>
        /// IDisposable interface.
        /// </summary>
        private bool _disposed;

        /// <summary>
        /// <para>
        /// Dispose() calls Dispose(true).
        /// Implement IDisposable. Do not make this method virtual.
        /// A derived class should not be able to override this method.
        /// </para>
        /// </summary>
        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);
        }

        /// <summary>
        /// <para>
        /// 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.
        /// </para>
        /// </summary>
        /// <param name="disposing">Whether it is directly called.</param>
        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
    }
}