Alright, so I'm having a bit of an issue here. Here is the loop.
lock (ClientLocker)
{
Trace.WriteLine("#WriteAll: " + sm.Header);
foreach (Client c in Clients)
{
if (c.LoggedIn)
{
Trace.WriteLine("#TryWriteTo[" + c.Id + "](" + sm.Header + ")");
LazyAsync.Invoke(() => c.WriteMessage(sm));
}
}
}
Here is LazyAsync
public static class LazyAsync
{
public static void Invoke(Action a)
{
a.BeginInvoke(a.EndInvoke, null);
}
}
Each Client
contains a socket
, so I can't hardly Clone
it.
The problem is, when I do the Invoke
to c.WriteMessage
, since the execution is delayed, it usually won't fire on the first couple in the list, and will sometimes actually only fire a whole bunch on the very last item.
I know this has to do with c being a reference that changes before Invoke
actually gets called, but is there a way to avoid this?
Doing a general for(int i=0 etc
loop doesn't seem to fix this issue.
Anyone have any ideas on how I can fix this?
Remember, can't Clone
Client
.