2018-02-13 09:23:53 -08:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
// Licensed under the MIT License.
|
2016-07-29 13:02:49 -07:00
|
|
|
|
2016-03-30 14:20:52 -07:00
|
|
|
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 ManualResetEvent _valueWasSet;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal T Value
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
bool result = _valueWasSet.WaitOne();
|
|
|
|
|
if (result == false)
|
|
|
|
|
{
|
|
|
|
|
_value = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_value = value;
|
|
|
|
|
_valueWasSet.Set();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructor for AsyncObject.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal AsyncObject()
|
|
|
|
|
{
|
|
|
|
|
_valueWasSet = new ManualResetEvent(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|