当前位置: 首页>>代码示例>>C#>>正文


C# Semaphore.Acquire方法代码示例

本文整理汇总了C#中Semaphore.Acquire方法的典型用法代码示例。如果您正苦于以下问题:C# Semaphore.Acquire方法的具体用法?C# Semaphore.Acquire怎么用?C# Semaphore.Acquire使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Semaphore的用法示例。


在下文中一共展示了Semaphore.Acquire方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AquireInSameThrad

 public void AquireInSameThrad()
 {
     Semaphore s = new Semaphore(2);
     Assert.AreEqual(2, s.Permits);
     s.Acquire();
     s.Acquire();
     Assert.AreEqual(0, s.Permits);
     
 }
开发者ID:ouyangyl,项目名称:MySpringNet,代码行数:9,代码来源:SemaphoreTest.cs

示例2: ReleaseMultipleTimesInSameThread

 public void ReleaseMultipleTimesInSameThread()
 {
     Semaphore s = new Semaphore(2);
     Assert.AreEqual(2, s.Permits);
     s.Acquire();
     s.Acquire();
     s.Release(2);
     Assert.AreEqual(2, s.Permits);            
 }
开发者ID:ouyangyl,项目名称:MySpringNet,代码行数:9,代码来源:SemaphoreTest.cs

示例3: AttemptWithNegativeMillisInSameThread

 public void AttemptWithNegativeMillisInSameThread()
 {
     Semaphore s = new Semaphore(2);
     Assert.AreEqual(2, s.Permits);
     s.Acquire();
     Assert.IsTrue(s.Attempt(-400));
     s.Release(2);
     Assert.AreEqual(2, s.Permits);            
 }
开发者ID:ouyangyl,项目名称:MySpringNet,代码行数:9,代码来源:SemaphoreTest.cs

示例4: Acquire

 public void Acquire()
 {
     //lock (_lock)
     //{
     //    --tokens;
     //    if (tokens >= 0) return;
     //}
     if (Interlocked.Decrement(ref tokens) >= 0) return;
     var sem = new Semaphore();
     waiting.Put(sem);
     sem.Acquire();
 }
开发者ID:BleuM937,项目名称:ConcurrentLibrary,代码行数:12,代码来源:FifoSemaphore.cs

示例5: UsingLikeAMutex

 public void UsingLikeAMutex ()
 {
     
     Semaphore semaphore = new Semaphore (1);
     Latch latch = new Latch ();
     Helper helper = new Helper (semaphore, latch);
     Thread thread = new Thread (new ThreadStart (helper.Go));
     semaphore.Acquire ();
     thread.Start ();
     latch.Acquire ();
     Assert.IsFalse (helper.gone);
     semaphore.Release ();
     thread.Join ();
     Assert.IsTrue (helper.gone, "not gone");
     
 }
开发者ID:ouyangyl,项目名称:MySpringNet,代码行数:16,代码来源:SemaphoreTest.cs

示例6: BossWorkerDemo

        public void BossWorkerDemo() 
        {
            int n = 10;

            Latch startPermit = new Latch();
            Semaphore wait = new Semaphore(-(n - 1));
            
            Worker [] workers = new Worker[n];
            for (int i=0; i<n; ++i) 
            {
                workers[i] = new Worker(startPermit, wait);
                new Thread(new ThreadStart(workers[i].Work)).Start();
            }
            // very slow main initialization ...
            // ... parse configuration
            // ... initialize other resources used by workers
            startPermit.Release();

            // now it is our turn to wait for workers
            wait.Acquire();

            for (int i=0; i<n; ++i) 
                Assert.IsTrue(workers[i].worked);
        }
开发者ID:spring-projects,项目名称:spring-net,代码行数:24,代码来源:LatchTest.cs

示例7: AcquireReleaseInDifferentThreads

 public void AcquireReleaseInDifferentThreads()
 {
     Semaphore s = new Semaphore(0); 
     AcquireReleaseWorker worker1 = new AcquireReleaseWorker(s);
     Thread thread1 = new Thread(new ThreadStart(worker1.DoWork));
     
     thread1.Start();
     Thread.Sleep(300);
     s.Release();
     s.Release();
     s.Acquire();
     s.Acquire();
     s.Release();
     thread1.Join();
     
 }
开发者ID:ouyangyl,项目名称:MySpringNet,代码行数:16,代码来源:SemaphoreTest.cs

示例8: AquireReleaseInSameThread

 public void AquireReleaseInSameThread()
 {
     Semaphore s = new Semaphore(1);            
     s.Acquire();
     s.Release();
     s.Acquire();
     s.Release();
     s.Acquire();
     s.Release();
     s.Acquire();
     s.Release();
     s.Acquire();
     s.Release();
     s.Acquire();
     s.Release();
     Assert.AreEqual(1, s.Permits);
 }
开发者ID:ouyangyl,项目名称:MySpringNet,代码行数:17,代码来源:SemaphoreTest.cs

示例9: AnInvalidApplicationIsNotUpdatedNorRemovedButWhenRemovedStillRaisesTheRemovedEvent

        public void AnInvalidApplicationIsNotUpdatedNorRemovedButWhenRemovedStillRaisesTheRemovedEvent ()
        {
            Semaphore sync = new Semaphore (0);
            AddApplication();
            InitHandlerAndStartLocation (sync);
            File.Delete(serviceXml);
            Assert.IsFalse(sync.Attempt(1000), "some events propagated, expecting no one");

            // remove
            TestUtils.SafeDeleteDirectory(sampleDir);
            Thread.SpinWait(1);
            Assert.IsFalse(Directory.Exists(sampleDir), "directory still exists");
            log.Debug("directory deleted");
            sync.Acquire ();
            log.Debug("sync acquired");
            Assert.IsFalse (Directory.Exists (sampleDir), "directory still exists: " + sampleDir);
            Assert.IsTrue (handler.applicationRemoved, "application not removed");
            Assert.AreEqual (0, location.Applications.Count);
        }
开发者ID:ouyangyl,项目名称:MySpringNet,代码行数:19,代码来源:FileSystemDeployLocationTest.cs

示例10: ReleaseMultipleBadArgument

 public void ReleaseMultipleBadArgument()
 {
     Semaphore s = new Semaphore(2);
     Assert.AreEqual(2, s.Permits);
     s.Acquire();
     s.Acquire();
     Assert.Throws<ArgumentOutOfRangeException>(() => s.Release(-2));
 }
开发者ID:spring-projects,项目名称:spring-net,代码行数:8,代码来源:SemaphoreTest.cs

示例11: RemoveAfterSpringAssemblyDeploy

 public void RemoveAfterSpringAssemblyDeploy ()
 {
     Semaphore sync = new Semaphore (0);
     InitHandlerAndStartLocation (sync);
     AddApplication();
     sync.Acquire();
     string springPrivateBin = Path.Combine(sampleDir, SpringAssembliesDeployer.PrivateBinPathPrefix);
     Directory.CreateDirectory(springPrivateBin);
     using (File.Create(Path.Combine(springPrivateBin, "foo.dll"))) {}
     Directory.Delete(sampleDir, true);
     Assert.IsFalse(Directory.Exists(sampleDir), "directory still exists");
     log.Debug (String.Format ("directory {0} removed to simulate application removal", sampleDir));
     sync.Acquire();
     Assert.IsTrue (handler.applicationRemoved, "application not removed");
     Assert.AreEqual (0, location.Applications.Count, "application not removed");
 }
开发者ID:ouyangyl,项目名称:MySpringNet,代码行数:16,代码来源:FileSystemDeployLocationTest.cs

示例12: RenamingADirectoryHostingAnApplicationItEqualsRemoveOldApplicationAndAddUnderNewName

 public void RenamingADirectoryHostingAnApplicationItEqualsRemoveOldApplicationAndAddUnderNewName ()
 {
     Semaphore sync = new Semaphore (0);
     InitHandlerAndStartLocation (sync);
     AddApplication ();
     sync.Acquire ();
     Assert.IsTrue (handler.applicationAdded, "application not added");
     Directory.Move (sampleDir, sampleDir2);
     sync.Acquire (); // add or remove
     sync.Acquire (); // add or remove
     Assert.IsTrue (handler.applicationRemoved, "application was not removed");
 }
开发者ID:ouyangyl,项目名称:MySpringNet,代码行数:12,代码来源:FileSystemDeployLocationTest.cs

示例13: RaiseEventRemovingAnApplication

 public void RaiseEventRemovingAnApplication ()
 {
     AddApplication ();
     Semaphore sync = new Semaphore (0);
     InitHandlerAndStartLocation (sync);
     Directory.Delete (sampleDir, true);
     Thread.SpinWait(1);
     Assert.IsFalse(Directory.Exists(sampleDir), "directory still exists");
     log.Debug("directory deleted");
     sync.Acquire ();
     log.Debug("sync acquired");
     Assert.IsFalse (Directory.Exists (sampleDir), "directory still exists: " + sampleDir);
     Assert.IsTrue (handler.applicationRemoved, "application not removed");
     Assert.AreEqual (0, location.Applications.Count);
 }
开发者ID:ouyangyl,项目名称:MySpringNet,代码行数:15,代码来源:FileSystemDeployLocationTest.cs

示例14: RaiseEventAddingAnApplication

 public void RaiseEventAddingAnApplication ()
 {
     Semaphore sync = new Semaphore (0);
     InitHandlerAndStartLocation (sync);
     AddApplication ();
     sync.Acquire ();
     Assert.IsTrue (handler.applicationAdded, "application not added");
     Assert.AreEqual (1, location.Applications.Count);
 }
开发者ID:ouyangyl,项目名称:MySpringNet,代码行数:9,代码来源:FileSystemDeployLocationTest.cs

示例15: IgnoreExceptionOnDispatcherWhenRemoving

 public void IgnoreExceptionOnDispatcherWhenRemoving ()
 {
     factoryMock.ExpectAndReturn("CreateApplicationWatcherMonitor", watcher);
     factoryMock.ExpectAndReturn("CreateApplicationWatcherMonitor", watcher);
     watcherMock.Expect("StartWatching");
     watcherMock.Expect("StopWatching");
     watcherMock.Expect("Dispose");
     watcherMock.Expect("StartWatching");
     watcherMock.Expect("StopWatching");
     watcherMock.Expect("Dispose");
     ISync canContinue = new Semaphore(0);
     ISync started = new Semaphore(0);
     AddApplication();
     AddApplication(sampleDir2);
     IDeployEventDispatcher dispatcher = new ExceptionDispatcher(started, canContinue);
     location = new FileSystemDeployLocation (dispatcher, factory, deployPath, true);
     Assert.AreEqual(2, location.Applications.Count);
     Directory.Delete(sampleDir, true);
     started.Acquire();
     canContinue.Release();
     Assert.AreEqual(1, location.Applications.Count);
     Directory.Delete(sampleDir2, true);
     started.Acquire();
     canContinue.Release();
     Assert.AreEqual(0, location.Applications.Count);
 }
开发者ID:ouyangyl,项目名称:MySpringNet,代码行数:26,代码来源:FileSystemDeployLocationTest.cs


注:本文中的Semaphore.Acquire方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。