1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-13 10:08:31 +00:00

Prevented concurrent list operations when modifying decorators

This commit is contained in:
Darth Affe 2019-11-21 20:38:38 +01:00
parent 86e9bb293b
commit 09e514c813
2 changed files with 28 additions and 9 deletions

View File

@ -78,9 +78,10 @@ namespace RGB.NET.Core
/// <param name="color">The <see cref="Color"/> to be modified.</param> /// <param name="color">The <see cref="Color"/> to be modified.</param>
protected virtual Color ApplyDecorators(Rectangle rectangle, BrushRenderTarget renderTarget, Color color) protected virtual Color ApplyDecorators(Rectangle rectangle, BrushRenderTarget renderTarget, Color color)
{ {
foreach (IBrushDecorator decorator in Decorators) lock (Decorators)
if (decorator.IsEnabled) foreach (IBrushDecorator decorator in Decorators)
color = decorator.ManipulateColor(rectangle, renderTarget, color); if (decorator.IsEnabled)
color = decorator.ManipulateColor(rectangle, renderTarget, color);
return color; return color;
} }

View File

@ -11,11 +11,20 @@ namespace RGB.NET.Core
{ {
#region Properties & Fields #region Properties & Fields
private List<T> _decorators = new List<T>(); private readonly List<T> _decorators = new List<T>();
/// <summary> /// <summary>
/// Gets a readonly-list of all <see cref="IDecorator"/> attached to this <see cref="IDecoratable{T}"/>. /// Gets a readonly-list of all <see cref="IDecorator"/> attached to this <see cref="IDecoratable{T}"/>.
/// </summary> /// </summary>
protected IReadOnlyCollection<T> Decorators => new ReadOnlyCollection<T>(_decorators); protected IReadOnlyCollection<T> Decorators { get; }
#endregion
#region Constructors
protected AbstractDecoratable()
{
Decorators = new ReadOnlyCollection<T>(_decorators);
}
#endregion #endregion
@ -24,8 +33,11 @@ namespace RGB.NET.Core
/// <inheritdoc /> /// <inheritdoc />
public void AddDecorator(T decorator) public void AddDecorator(T decorator)
{ {
_decorators.Add(decorator); lock (Decorators)
_decorators = _decorators.OrderByDescending(x => x.Order).ToList(); {
_decorators.Add(decorator);
_decorators.Sort((d1, d2) => d1.Order.CompareTo(d2.Order));
}
decorator.OnAttached(this); decorator.OnAttached(this);
} }
@ -33,7 +45,8 @@ namespace RGB.NET.Core
/// <inheritdoc /> /// <inheritdoc />
public void RemoveDecorator(T decorator) public void RemoveDecorator(T decorator)
{ {
_decorators.Remove(decorator); lock (Decorators)
_decorators.Remove(decorator);
decorator.OnDetached(this); decorator.OnDetached(this);
} }
@ -41,7 +54,12 @@ namespace RGB.NET.Core
/// <inheritdoc /> /// <inheritdoc />
public void RemoveAllDecorators() public void RemoveAllDecorators()
{ {
foreach (T decorator in Decorators.ToList()) IEnumerable<T> decorators;
lock (Decorators)
decorators = Decorators.ToList();
foreach (T decorator in decorators)
RemoveDecorator(decorator); RemoveDecorator(decorator);
} }