File size: 1,327 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Threading;

using Dbg = System.Management.Automation.Diagnostics;

namespace System.Management.Automation.Remoting
{
    /// <summary>
    /// Blocks caller trying to get the value of an object of type T
    /// until the value is set. After the set all future gets are
    /// unblocked.
    /// </summary>
    internal class AsyncObject<T> where T : class
    {
        /// <summary>
        /// Value.
        /// </summary>
        private T _value;

        /// <summary>
        /// Value was set.
        /// </summary>
        private readonly ManualResetEvent _valueWasSet;

        /// <summary>
        /// Value.
        /// </summary>
        internal T Value
        {
            get
            {
                bool result = _valueWasSet.WaitOne();
                if (!result)
                {
                    _value = null;
                }

                return _value;
            }

            set
            {
                _value = value;
                _valueWasSet.Set();
            }
        }

        /// <summary>
        /// Constructor for AsyncObject.
        /// </summary>
        internal AsyncObject()
        {
            _valueWasSet = new ManualResetEvent(false);
        }
    }
}