예제 #1
0
        private async Task LoadValueWithRetryAsync()
        {
            _updateIsInProgress.Value = true;
            var exceptions = new List <Exception>();

            try
            {
                for (var i = 0; i <= _maxRetries; i++)
                {
                    try
                    {
                        _value = await _factory(_value).ConfigureAwait(false);
                    }
                    catch (Exception e)
                    {
                        exceptions.Add(e);
                        FactoryError?.BeginRaiseEvent(this, e);
                        continue;
                    }

                    ValueCreated?.BeginRaiseEvent(this, _value);
                    return;
                }

                MaxRetriesExceeded?.BeginRaiseEvent(this, exceptions);
            }
            finally
            {
                _updateIsInProgress.Value = false;
            }
        }
예제 #2
0
        /// <summary>Gets an existing or creates a new cached value.</summary>
        /// <param name="valueFactory">The method that creates the value when it is null.</param>
        /// <returns>The value.</returns>
        public virtual T GetValue(Func <T> valueFactoryMethod)
        {
            if (Value == null)
            {
                lock (this)
                {
                    if (Value != null)
                    {
                        return(Value);
                    }

                    Value = valueFactoryMethod();
                    if (ValueCreated != null)
                    {
                        ValueCreated.Invoke(this, new ValueEventArgs <T> {
                            Value = Value
                        });
                    }
                }
            }

            return(Value);
        }
예제 #3
0
파일: MyLazy.cs 프로젝트: j0no89/Onwo
 protected virtual void OnValueCreated()
 {
     ValueCreated?.Invoke(this);
 }