diff --git a/Lidgren.Network/NetPeer.Internal.cs b/Lidgren.Network/NetPeer.Internal.cs
index 8cb528d..1aaf5e2 100644
--- a/Lidgren.Network/NetPeer.Internal.cs
+++ b/Lidgren.Network/NetPeer.Internal.cs
@@ -47,13 +47,15 @@ namespace Lidgren.Network
///
/// Call this to register a callback for when a new message arrives
///
- public void RegisterReceivedCallback(SendOrPostCallback callback)
+ public void RegisterReceivedCallback(SendOrPostCallback callback, SynchronizationContext syncContext = null)
{
- if (SynchronizationContext.Current == null)
+ if (syncContext == null)
+ syncContext = SynchronizationContext.Current;
+ if (syncContext == null)
throw new NetException("Need a SynchronizationContext to register callback on correct thread!");
if (m_receiveCallbacks == null)
m_receiveCallbacks = new List>();
- m_receiveCallbacks.Add(new NetTuple(SynchronizationContext.Current, callback));
+ m_receiveCallbacks.Add(new NetTuple(syncContext, callback));
}
///
@@ -63,7 +65,17 @@ namespace Lidgren.Network
{
if (m_receiveCallbacks == null)
return;
- m_receiveCallbacks.Remove(new NetTuple(SynchronizationContext.Current, callback));
+
+ // remove all callbacks regardless of sync context
+ RestartRemoveCallbacks:
+ for (int i = 0; i < m_receiveCallbacks.Count; i++)
+ {
+ if (m_receiveCallbacks[i].Item2.Equals(callback))
+ {
+ m_receiveCallbacks.RemoveAt(i);
+ goto RestartRemoveCallbacks;
+ }
+ }
if (m_receiveCallbacks.Count < 1)
m_receiveCallbacks = null;
}