summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Events/ManagedEvent.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-09-07 13:06:27 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-09-07 13:06:27 -0400
commit5e43bdbf5cd6dbab36c25287c85d42ccfeea2c83 (patch)
tree0a42305174eb84561a584549cd685c5e95670f36 /src/SMAPI/Framework/Events/ManagedEvent.cs
parent8da88b8fe5b41739c5cd0df3280b9770fc7f10a4 (diff)
parentf9fac11028354f15d786d5b854608edb10716f79 (diff)
downloadSMAPI-5e43bdbf5cd6dbab36c25287c85d42ccfeea2c83.tar.gz
SMAPI-5e43bdbf5cd6dbab36c25287c85d42ccfeea2c83.tar.bz2
SMAPI-5e43bdbf5cd6dbab36c25287c85d42ccfeea2c83.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/Events/ManagedEvent.cs')
-rw-r--r--src/SMAPI/Framework/Events/ManagedEvent.cs43
1 files changed, 26 insertions, 17 deletions
diff --git a/src/SMAPI/Framework/Events/ManagedEvent.cs b/src/SMAPI/Framework/Events/ManagedEvent.cs
index 8b25a9b5..f2dfb2ab 100644
--- a/src/SMAPI/Framework/Events/ManagedEvent.cs
+++ b/src/SMAPI/Framework/Events/ManagedEvent.cs
@@ -70,27 +70,33 @@ namespace StardewModdingAPI.Framework.Events
/// <param name="mod">The mod which added the event handler.</param>
public void Add(EventHandler<TEventArgs> handler, IModMetadata mod)
{
- EventPriority priority = handler.Method.GetCustomAttribute<EventPriorityAttribute>()?.Priority ?? EventPriority.Normal;
- var managedHandler = new ManagedEventHandler<TEventArgs>(handler, this.RegistrationIndex++, priority, mod);
+ lock (this.Handlers)
+ {
+ EventPriority priority = handler.Method.GetCustomAttribute<EventPriorityAttribute>()?.Priority ?? EventPriority.Normal;
+ var managedHandler = new ManagedEventHandler<TEventArgs>(handler, this.RegistrationIndex++, priority, mod);
- this.Handlers.Add(managedHandler);
- this.CachedHandlers = null;
- this.HasNewHandlers = true;
+ this.Handlers.Add(managedHandler);
+ this.CachedHandlers = null;
+ this.HasNewHandlers = true;
+ }
}
/// <summary>Remove an event handler.</summary>
/// <param name="handler">The event handler.</param>
public void Remove(EventHandler<TEventArgs> handler)
{
- // match C# events: if a handler is listed multiple times, remove the last one added
- for (int i = this.Handlers.Count - 1; i >= 0; i--)
+ lock (this.Handlers)
{
- if (this.Handlers[i].Handler != handler)
- continue;
+ // match C# events: if a handler is listed multiple times, remove the last one added
+ for (int i = this.Handlers.Count - 1; i >= 0; i--)
+ {
+ if (this.Handlers[i].Handler != handler)
+ continue;
- this.Handlers.RemoveAt(i);
- this.CachedHandlers = null;
- break;
+ this.Handlers.RemoveAt(i);
+ this.CachedHandlers = null;
+ break;
+ }
}
}
@@ -106,14 +112,17 @@ namespace StardewModdingAPI.Framework.Events
// update cached data
// (This is debounced here to avoid repeatedly sorting when handlers are added/removed,
// and keeping a separate cached list allows changes during enumeration.)
- var handlers = this.CachedHandlers; // iterate local copy in case a mod adds/removes a handler while handling the event
+ var handlers = this.CachedHandlers; // iterate local copy in case a mod adds/removes a handler while handling the event, which will set this field to null
if (handlers == null)
{
- if (this.HasNewHandlers && this.Handlers.Any(p => p.Priority != EventPriority.Normal))
- this.Handlers.Sort();
+ lock (this.Handlers)
+ {
+ if (this.HasNewHandlers && this.Handlers.Any(p => p.Priority != EventPriority.Normal))
+ this.Handlers.Sort();
- this.CachedHandlers = handlers = this.Handlers.ToArray();
- this.HasNewHandlers = false;
+ this.CachedHandlers = handlers = this.Handlers.ToArray();
+ this.HasNewHandlers = false;
+ }
}
// raise event