Make event listeners thread safe
This commit is contained in:
parent
2729418006
commit
096c42adbc
|
@ -51,12 +51,14 @@ namespace Ooui
|
||||||
var sendListen = false;
|
var sendListen = false;
|
||||||
|
|
||||||
List<EventHandler> handlers;
|
List<EventHandler> handlers;
|
||||||
|
lock (eventListeners) {
|
||||||
if (!eventListeners.TryGetValue (eventType, out handlers)) {
|
if (!eventListeners.TryGetValue (eventType, out handlers)) {
|
||||||
handlers = new List<EventHandler> ();
|
handlers = new List<EventHandler> ();
|
||||||
eventListeners[eventType] = handlers;
|
eventListeners[eventType] = handlers;
|
||||||
sendListen = true;
|
sendListen = true;
|
||||||
}
|
}
|
||||||
handlers.Add (handler);
|
handlers.Add (handler);
|
||||||
|
}
|
||||||
|
|
||||||
if (sendListen)
|
if (sendListen)
|
||||||
Send (new Message {
|
Send (new Message {
|
||||||
|
@ -72,10 +74,12 @@ namespace Ooui
|
||||||
if (handler == null) return;
|
if (handler == null) return;
|
||||||
|
|
||||||
List<EventHandler> handlers;
|
List<EventHandler> handlers;
|
||||||
|
lock (eventListeners) {
|
||||||
if (eventListeners.TryGetValue (eventType, out handlers)) {
|
if (eventListeners.TryGetValue (eventType, out handlers)) {
|
||||||
handlers.Remove (handler);
|
handlers.Remove (handler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected bool SetProperty<T> (ref T backingStore, T newValue, string attributeName, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
|
protected bool SetProperty<T> (ref T backingStore, T newValue, string attributeName, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
|
||||||
{
|
{
|
||||||
|
@ -169,15 +173,20 @@ namespace Ooui
|
||||||
|
|
||||||
protected virtual void TriggerEventFromMessage (Message message)
|
protected virtual void TriggerEventFromMessage (Message message)
|
||||||
{
|
{
|
||||||
List<EventHandler> handlers;
|
List<EventHandler> handlers = null;
|
||||||
if (eventListeners.TryGetValue (message.Key, out handlers)) {
|
lock (eventListeners) {
|
||||||
|
List<EventHandler> hs;
|
||||||
|
if (eventListeners.TryGetValue (message.Key, out hs)) {
|
||||||
|
handlers = new List<EventHandler> (hs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (handlers == null) return;
|
||||||
var args = EventArgs.Empty;
|
var args = EventArgs.Empty;
|
||||||
foreach (var h in handlers) {
|
foreach (var h in handlers) {
|
||||||
h.Invoke (this, args);
|
h.Invoke (this, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
class EventTargetJsonConverter : Newtonsoft.Json.JsonConverter
|
class EventTargetJsonConverter : Newtonsoft.Json.JsonConverter
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue