diff --git a/RGB.NET.Core/Brushes/AbstractBrush.cs b/RGB.NET.Core/Brushes/AbstractBrush.cs index 0c6591a..7e36afc 100644 --- a/RGB.NET.Core/Brushes/AbstractBrush.cs +++ b/RGB.NET.Core/Brushes/AbstractBrush.cs @@ -78,9 +78,10 @@ namespace RGB.NET.Core /// The to be modified. protected virtual Color ApplyDecorators(Rectangle rectangle, BrushRenderTarget renderTarget, Color color) { - foreach (IBrushDecorator decorator in Decorators) - if (decorator.IsEnabled) - color = decorator.ManipulateColor(rectangle, renderTarget, color); + lock (Decorators) + foreach (IBrushDecorator decorator in Decorators) + if (decorator.IsEnabled) + color = decorator.ManipulateColor(rectangle, renderTarget, color); return color; } diff --git a/RGB.NET.Core/Decorators/AbstractDecorateable.cs b/RGB.NET.Core/Decorators/AbstractDecorateable.cs index a9c5560..94b8f05 100644 --- a/RGB.NET.Core/Decorators/AbstractDecorateable.cs +++ b/RGB.NET.Core/Decorators/AbstractDecorateable.cs @@ -11,11 +11,20 @@ namespace RGB.NET.Core { #region Properties & Fields - private List _decorators = new List(); + private readonly List _decorators = new List(); /// /// Gets a readonly-list of all attached to this . /// - protected IReadOnlyCollection Decorators => new ReadOnlyCollection(_decorators); + protected IReadOnlyCollection Decorators { get; } + + #endregion + + #region Constructors + + protected AbstractDecoratable() + { + Decorators = new ReadOnlyCollection(_decorators); + } #endregion @@ -24,8 +33,11 @@ namespace RGB.NET.Core /// public void AddDecorator(T decorator) { - _decorators.Add(decorator); - _decorators = _decorators.OrderByDescending(x => x.Order).ToList(); + lock (Decorators) + { + _decorators.Add(decorator); + _decorators.Sort((d1, d2) => d1.Order.CompareTo(d2.Order)); + } decorator.OnAttached(this); } @@ -33,7 +45,8 @@ namespace RGB.NET.Core /// public void RemoveDecorator(T decorator) { - _decorators.Remove(decorator); + lock (Decorators) + _decorators.Remove(decorator); decorator.OnDetached(this); } @@ -41,7 +54,12 @@ namespace RGB.NET.Core /// public void RemoveAllDecorators() { - foreach (T decorator in Decorators.ToList()) + IEnumerable decorators; + + lock (Decorators) + decorators = Decorators.ToList(); + + foreach (T decorator in decorators) RemoveDecorator(decorator); }