Hello,
I work with .net remoting,to simplify, when the client connected, he send an
handle to an object, the object is used to exchange message and events. no
problem.
In my server, I keep a list of all actives clients and the handle of each.,
no problem
In remoting, it's difficult to see when a client is disconnected, so I use
asynchronous call to contact the client and if I got an error when I call
EndInvoke =the client is disconnected, I remove the handle for the object,
no problem.
But in a function, I wan't to loop into each connected client and call a
function on each client.
For that, I do a foreach on my collection of clients, and I call the
function.
BUT I call asynchronously the function, so, when the call is terminated, it
check for the existense of the client, if problem, the client is removed
from the list
=The list is modified AND a I would do a foreach on the collection =>
Sometime, BUG.
I tried to do a lock on the collection (see the code above), but that don't
work. the client is (sometime) removed before the foreach is at the end.
How can I block the remove when I'm in the foreach ?
Thx
public void
AttachMessageHandler(Dufour.InterfaceBascule.Messa geHandlerBascule MsgHnd)
{
lock (RemoteClients)
{
foreach (DictionaryEntry Client in RemoteClients)
{
AsyncDelegateCurrentPesee d = new
AsyncDelegateCurrentPesee(((Dufour.InterfaceBascul e.MessageHandlerBascule)Client.Value).OnServerEven tCurrentPoids);
PoolCallback p = new PoolCallback((String)Client.Key, d);
d.BeginInvoke(LastPesee, CallbackTerminatedCurrentPesee, p);
}
}
public void CallbackTerminatedCurrentPesee(IAsyncResult ar)
{
try
{
AsyncDelegateCurrentPesee d =
((AsyncDelegateCurrentPesee)((PoolCallback)ar.Asyn cState).Delegate);
d.EndInvoke(ar);
}
catch (Exception)
{
lock (RemoteClients)
{
RemoteClients.Remove(((PoolCallback)ar.AsyncState) .uri);
}
}
}