Couchbase.ObserveHandler.performParallelObserve C# (CSharp) Method

performParallelObserve() private method

private performParallelObserve ( ICouchbaseServerPool pool ) : IObserveOperationResult
pool ICouchbaseServerPool
return IObserveOperationResult
        private IObserveOperationResult performParallelObserve(ICouchbaseServerPool pool)
        {
            var commandConfig = setupObserveOperation(pool);
            var observedNodes = commandConfig.Item2.Select(n => new ObservedNode
            {
                Node = n as CouchbaseNode,
                IsMaster = n == commandConfig.Item2[0]
            }).ToArray();

            var replicaFoundCount = 0;
            var replicaPersistedCount = 0;
            var isKeyPersistedToMaster = false;

            IObserveOperationResult result = new ObserveOperationResult();

            do
            {
                var are = new AutoResetEvent(false);
                var timer = new Timer(state =>
                {
                    result = checkNodesForKey(observedNodes, commandConfig.Item3, ref isKeyPersistedToMaster, ref replicaFoundCount, ref replicaPersistedCount);

                    if (result.Message == ObserveOperationConstants.MESSAGE_MODIFIED)
                    {
                        are.Set();
                        result.Fail(ObserveOperationConstants.MESSAGE_MODIFIED);
                    }
                    else if (isInExpectedState(replicaFoundCount, replicaPersistedCount, isKeyPersistedToMaster))
                    {
                        are.Set();
                        result.Pass();
                    }

                }, are, 0, 500);

                if (!are.WaitOne(_settings.Timeout))
                {
                    timer.Change(-1, -1);
                    result.Fail(ObserveOperationConstants.MESSAGE_TIMEOUT, new TimeoutException());
                    return result;
                }

                if (result.Success)
                {
                    timer.Change(-1, -1);
                }

            } while (result.Message == string.Empty && !isInExpectedState(replicaFoundCount, replicaPersistedCount, isKeyPersistedToMaster));

            return result;
        }