开发者

Why variable value is getting changed unusually

I have a code like this,In this context HandleDisconnectEventCB will trigger when client has some modified whithout saved data and disconnected(remove network cable) from server,,then it will make 'clientWithLock= 0' and will remove from collection that is shown below code. But now the problem is other connected clients cannot make changes,it showing that 'clientWithLock' has some data not zero,due to that it will show another user is updating data please wait.Is there any badlogic going on here

private static int clientWithLock = 0;

static private void HandleDisconnectEventCB(SPD.SPD_serverL开发者_开发问答ocationType loc,
                                            string server, int clientId,
                                            object passback)
{
    // Remove lock if necessary
    if (clientWithLock == clientId) clientWithLock = 0;

    // Remove client from client list and end replicated display sessions
    for (int i = 0; i < clients.Count; i++)
    {
        SPURTclient sc = (SPURTclient)clients[i];
        if (sc.ClientId == clientId)
        {
            .
            .
            clients.RemoveAt(i);
            break;


Making clientwithlock static is going to cause you all kinds of headaches here if you have multiple disconnects in rapid succession. I'd recommend either making the whole operation stateless, so 'ClientWithLock' is passed as a parameter to the event, and you operate the static function based on all parameters passed. If 'ClientWithLock' is updated twice in fast succession for disconnects, behaviour will be quite strange.

Or, instancing the whole class and operation so the disconnection handler is instanced on a class by class basis if having the whole thing stateless is not an option for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜