Renci.SshNet.Common.SemaphoreLight.Wait C# (CSharp) Method

Wait() public method

Blocks the current thread until it can enter the SemaphoreLight.
public Wait ( ) : void
return void
        public void Wait()
        {

            lock (_lock)
            {
                while (_currentCount < 1)
                {
                    Monitor.Wait(_lock);
                }

                _currentCount--;

                Monitor.Pulse(_lock);
            }
        }
    }

Usage Example

示例#1
0
        public void WaitTest()
        {
            const int sleepTime = 200; 
            const int initialCount = 2;
            var target = new SemaphoreLight(initialCount);

            var start = DateTime.Now;

            target.Wait();
            target.Wait();

            Assert.IsTrue((DateTime.Now - start).TotalMilliseconds < 50);

            var releaseThread = new Thread(
                () =>
                    {
                        Thread.Sleep(sleepTime);
                        target.Release();
                    });
            releaseThread.Start();

            target.Wait();

            var end = DateTime.Now;
            var elapsed = end - start;

            Assert.IsTrue(elapsed.TotalMilliseconds > 200);
            Assert.IsTrue(elapsed.TotalMilliseconds < 250);
        }
All Usage Examples Of Renci.SshNet.Common.SemaphoreLight::Wait