collection modified, lock ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Geoffrey

    collection modified, lock ?

    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
    AttachMessageHa ndler(Dufour.In terfaceBascule. MessageHandlerB ascule MsgHnd)

    {

    lock (RemoteClients)

    {

    foreach (DictionaryEntr y Client in RemoteClients)

    {

    AsyncDelegateCu rrentPesee d = new
    AsyncDelegateCu rrentPesee(((Du four.InterfaceB ascule.MessageH andlerBascule)C lient.Value).On ServerEventCurr entPoids);

    PoolCallback p = new PoolCallback((S tring)Client.Ke y, d);

    d.BeginInvoke(L astPesee, CallbackTermina tedCurrentPesee , p);

    }

    }



    public void CallbackTermina tedCurrentPesee (IAsyncResult ar)

    {

    try

    {

    AsyncDelegateCu rrentPesee d =
    ((AsyncDelegate CurrentPesee)(( PoolCallback)ar .AsyncState).De legate);

    d.EndInvoke(ar) ;

    }

    catch (Exception)

    {

    lock (RemoteClients)

    {

    RemoteClients.R emove(((PoolCal lback)ar.AsyncS tate).uri);

    }

    }

    }


Working...