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; } }
/// <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); }
protected virtual void OnValueCreated() { ValueCreated?.Invoke(this); }