public void Dispose()
        {
            if (_connection == null)
            {
                return;
            }

            Logger.Debug("Closing ZkClient...");
            EventLock.Lock();
            try
            {
                ShutdownTrigger = true;
                _eventThread.Interrupt();
                _eventThread.Join(2000);
                _connection.Dispose();
                _connection = null;
            }
            catch (ThreadInterruptedException e)
            {
                throw new ZkInterruptedException(e);
            }
            finally
            {
                EventLock.Unlock();
            }

            Logger.Debug("Closing ZkClient...done");
        }
        public bool WaitForKeeperState(KeeperState keeperState, TimeSpan timeout)
        {
            if (_zookeeperEventThread != null && Thread.CurrentThread == _zookeeperEventThread)
            {
                throw new Exception("Must not be done in the zookeeper event thread.");
            }

            Logger.Debug("Waiting for keeper state " + keeperState);
            this.AcquireEventLock();
            try
            {
                bool stillWaiting = true;
                while (CurrentState != keeperState)
                {
                    if (!stillWaiting)
                    {
                        return(false);
                    }

                    stillWaiting = EventLock.StateChangedCondition.Await(timeout);
                }

                Logger.Debug("State is " + CurrentState);
                return(true);
            }
            catch (ThreadInterruptedException e)
            {
                throw new ZkInterruptedException(e);
            }
            finally
            {
                EventLock.Unlock();
            }
        }
        public bool WaitUntilExists(String path, TimeSpan time)
        {
            DateTime timeout = DateTime.Now + time;

            Logger.Debug("Waiting until znode '" + path + "' becomes available.");
            if (Exists(path))
            {
                return(true);
            }

            AcquireEventLock();
            try
            {
                while (!Exists(path, true))
                {
                    var gotSignal = EventLock.ZNodeEventCondition.AwaitUntil(timeout);
                    if (!gotSignal)
                    {
                        return(false);
                    }
                }

                return(true);
            }
            catch (ThreadInterruptedException e)
            {
                throw new ZkInterruptedException("Thread interrupted", e);
            }
            finally
            {
                EventLock.Unlock();
            }
        }
 private void Reconnect()
 {
     EventLock.Lock();
     try
     {
         _connection.Dispose();
         _connection.Connect(this);
     }
     catch (ThreadInterruptedException e)
     {
         throw new ZkInterruptedException(e);
     }
     finally
     {
         EventLock.Unlock();
     }
 }
 public long GetCreationTime(String path)
 {
     try
     {
         EventLock.LockInterruptibly();
         return(_connection.GetCreateTime(path));
     }
     catch (KeeperException e)
     {
         throw ZkException.Create(e);
     }
     catch (ThreadInterruptedException e)
     {
         throw new ZkInterruptedException(e);
     }
     finally
     {
         EventLock.Unlock();
     }
 }
        public void Connect(long maxMsToWaitUntilConnected, IWatcher watcher)
        {
            bool started = false;

            try
            {
                EventLock.LockInterruptibly();
                ShutdownTrigger = false;
                _eventThread    = new ZkEventThread(_connection.Servers);
                _eventThread.Start();
                _connection.Connect(watcher);

                Logger.Debug("Awaiting connection to Zookeeper server");
                if (!WaitUntilConnected(TimeSpan.FromMilliseconds(maxMsToWaitUntilConnected)))
                {
                    throw new ZkTimeoutException("Unable to connect to zookeeper server within timeout: " + maxMsToWaitUntilConnected);
                }

                started = true;
            }
            catch (ThreadInterruptedException)
            {
                ZooKeeper.States state = _connection.ZookeeperState;
                throw new Exception("Not connected with zookeeper server yet. Current state is " + state);
            }
            finally
            {
                EventLock.Unlock();

                // we should close the zookeeper instance, otherwise it would keep
                // on trying to connect
                if (!started)
                {
                    this.Dispose();
                }
            }
        }