示例#1
0
        private void CreateValue()
        {
            ThrowIfDisposedOrDisposing(_state, nameof(ValueLazy <T>));

            var spinWait = new SpinWait();

            while (!IsValueCreated)
            {
                var previousState = _state.TryTransition(from: Initialized, to: Creating);

                if (previousState == Initialized)
                {
                    AssertNotNull(_factory);

                    try
                    {
                        _value = _factory();
                        _state.Transition(from: Creating, to: Created);
                    }
                    catch
                    {
                        _ = _state.Transition(to: Faulted);
                        throw;
                    }

                    _factory = null;
                }
                else
                {
                    spinWait.SpinOnce();
                }
            }
        }
示例#2
0
 /// <summary>Requests that the instance exits the event loop.</summary>
 /// <remarks>
 ///   <para>This method does nothing if <see cref="IsRunning" /> is <c>false</c>.</para>
 ///   <para>This method can be called from any thread.</para>
 /// </remarks>
 public void RequestExit() => _ = _state.TryTransition(from: Running, to: Exiting);