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


C# Semaphore.Release方法代码示例

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


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

示例1: Run

    private int Run(int iCount, int mCount, string semName, int iRandom)
    {
        // Testing createdNew
        bool createdNew;
        int iRet = -1, count = iCount;
        Semaphore sem = null;
        if (iRandom > 0)
            semName = Common.GenerateUnicodeString(iRandom);
        try
        {
            using(sem = new Semaphore(iCount, mCount, semName, out createdNew))
            {
                if (iCount > 0)
                {
                    sem.WaitOne();
                    count--;
                }

                int iPrev = sem.Release();
                if (iPrev == count && createdNew)
                    iRet = 100;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Unexpected exception thrown:  " + ex.ToString());
        }

        Console.WriteLine(100 == iRet ? "Test Passed" : "Test Failed");
        return iRet;
    }
开发者ID:CheneyWu,项目名称:coreclr,代码行数:31,代码来源:semaphorector4.cs

示例2: Run

    private int Run(int iCount, int mCount)
    {
        // Testing basic scenario
        int iRet = -1;
        Semaphore sem = null;
        try
        {
            using (sem = new Semaphore(iCount, mCount))
            {
                int iPrev = 0, count = iCount;
                // Do a wait one if we can
                if (iCount > 0)
                {
                   sem.WaitOne();
                   count--;
                }
                iPrev = sem.Release();

                if (iPrev == count)
                   iRet = 100;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("FAIL: CtorTest1(" + iCount + "," + mCount + ") - Unexpected exception thrown:  " + ex.ToString());
        }
        Console.WriteLine(100 == iRet ? "Test Passed" : "Test Failed");
        return iRet;
    }
开发者ID:CheneyWu,项目名称:coreclr,代码行数:29,代码来源:semaphorector1.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: 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

示例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: runExample

    public static void runExample()
    {
        // Create a semaphore that can satisfy up to three
        // concurrent requests. Use an initial count of zero,
        // so that the entire semaphore count is initially
        // owned by the main program thread.
        //
        _pool = new Semaphore(0, 3);

        // Create and start five numbered threads.
        //
        for(int i = 1; i <= 5; i++)
        {
            Thread t = new Thread(new ParameterizedThreadStart(Worker));

            // Start the thread, passing the number.
            //
            t.Start(i);
        }

        // Wait for half a second, to allow all the
        // threads to start and to block on the semaphore.
        //
        Thread.Sleep(500);

        // The main thread starts out holding the entire
        // semaphore count. Calling Release(3) brings the
        // semaphore count back to its maximum value, and
        // allows the waiting threads to enter the semaphore,
        // up to three at a time.
        //
        Console.WriteLine("Main thread calls Release(3).");
        _pool.Release(3);

        Console.WriteLine("Main thread exits.");
    }
开发者ID:bkleinen,项目名称:ConcurrencyExamples,代码行数:36,代码来源:ThreadExample-MSDN-Semaphore.cs

示例7: Run

    private int Run(int iCount, int mCount, string semName, int iRandom)
    {
        // Testing createdNew
        bool createdNew, createdNew2;
        bool bResult = true;
        int iRet = -1, count = iCount;
        Semaphore sem1 = null, sem2 = null;
        if (iRandom > 0)
            semName = Common.GenerateUnicodeString(iRandom);
        try
        {
            //  Open one, createdNew = true
            using (sem1 = new Semaphore(iCount, mCount, semName, out createdNew))
            {
                if (!createdNew)
                    bResult = false;
                //  Open another, createdNew = false
                using (sem2 = new Semaphore(iCount, mCount, semName, out createdNew2))
                {
                    if (createdNew2)
                        bResult = false;

                    if (iCount > 0)
                    {
                        sem2.WaitOne();
                        count--;
                    }

                    int iPrev = sem2.Release();
                    if (bResult && iPrev == count)
                        iRet = 100;
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Unexpected exception thrown:  " + ex.ToString());
        }

        Console.WriteLine(100 == iRet ? "Test Passed" : "Test Failed");
        return iRet;
    }
开发者ID:CheneyWu,项目名称:coreclr,代码行数:42,代码来源:semaphorector5.cs

示例8: OpenExisting_SameAsOriginal_Windows

        public void OpenExisting_SameAsOriginal_Windows()
        {
            string name = Guid.NewGuid().ToString("N");
            bool createdNew;
            using (Semaphore s1 = new Semaphore(0, Int32.MaxValue, name, out createdNew))
            {
                Assert.True(createdNew);

                using (Semaphore s2 = Semaphore.OpenExisting(name))
                {
                    Assert.False(s1.WaitOne(0));
                    Assert.False(s2.WaitOne(0));
                    s1.Release();
                    Assert.True(s2.WaitOne(0));
                    Assert.False(s2.WaitOne(0));
                    s2.Release();
                    Assert.True(s1.WaitOne(0));
                    Assert.False(s1.WaitOne(0));
                }

                Semaphore s3;
                Assert.True(Semaphore.TryOpenExisting(name, out s3));
                using (s3)
                {
                    Assert.False(s1.WaitOne(0));
                    Assert.False(s3.WaitOne(0));
                    s1.Release();
                    Assert.True(s3.WaitOne(0));
                    Assert.False(s3.WaitOne(0));
                    s3.Release();
                    Assert.True(s1.WaitOne(0));
                    Assert.False(s1.WaitOne(0));
                }
            }
        }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:35,代码来源:SemaphoreTests.cs

示例9: CanWaitWithoutBlockingForReleasedCount

        public void CanWaitWithoutBlockingForReleasedCount()
        {
            using (Semaphore s = new Semaphore(0, Int32.MaxValue))
            {
                for (int counts = 1; counts < 5; counts++)
                {
                    Assert.False(s.WaitOne(0));

                    if (counts % 2 == 0)
                    {
                        for (int i = 0; i < counts; i++)
                            s.Release();
                    }
                    else
                    {
                        s.Release(counts);
                    }

                    for (int i = 0; i < counts; i++)
                    {
                        Assert.True(s.WaitOne(0));
                    }

                    Assert.False(s.WaitOne(0));
                }
            }
        }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:27,代码来源:SemaphoreTests.cs

示例10: AnonymousProducerConsumer

 public void AnonymousProducerConsumer()
 {
     using (Semaphore s = new Semaphore(0, Int32.MaxValue))
     {
         const int NumItems = 5;
         Task.WaitAll(
             Task.Factory.StartNew(() =>
             {
                 for (int i = 0; i < NumItems; i++)
                     Assert.True(s.WaitOne(FailedWaitTimeout));
                 Assert.False(s.WaitOne(0));
             }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default),
             Task.Factory.StartNew(() =>
             {
                 for (int i = 0; i < NumItems; i++)
                     s.Release();
             }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default));
 }
 }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:19,代码来源:SemaphoreTests.cs

示例11: NamedProducerConsumer

 public void NamedProducerConsumer()
 {
     string name = Guid.NewGuid().ToString("N");
     const int NumItems = 5;
     var b = new Barrier(2);
     Task.WaitAll(
         Task.Factory.StartNew(() =>
         {
             using (var s = new Semaphore(0, int.MaxValue, name))
             {
                 Assert.True(b.SignalAndWait(FailedWaitTimeout));
                 for (int i = 0; i < NumItems; i++)
                     Assert.True(s.WaitOne(FailedWaitTimeout));
                 Assert.False(s.WaitOne(0));
             }
         }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default),
         Task.Factory.StartNew(() =>
         {
             using (var s = new Semaphore(0, int.MaxValue, name))
             {
                 Assert.True(b.SignalAndWait(FailedWaitTimeout));
                 for (int i = 0; i < NumItems; i++)
                     s.Release();
             }
         }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default));
 }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:26,代码来源:SemaphoreTests.cs

示例12: AttemptInSameThread

        public void AttemptInSameThread()
        {
            Semaphore s = new Semaphore(1);
            Assert.IsTrue(s.Attempt(SHORT_DELAY_MS));
            s.Release();
            Assert.IsTrue(s.Attempt(SHORT_DELAY_MS));
            s.Release();
            Assert.IsTrue(s.Attempt(SHORT_DELAY_MS));
            s.Release();
            Assert.IsTrue(s.Attempt(SHORT_DELAY_MS));
            s.Release();
            Assert.IsTrue(s.Attempt(SHORT_DELAY_MS));
            s.Release();
            Assert.AreEqual(1, s.Permits);

        }
开发者ID:ouyangyl,项目名称:MySpringNet,代码行数:16,代码来源:SemaphoreTest.cs

示例13: Release

        public void Release()
        {
            using (Semaphore s = new Semaphore(1, 1))
            {
                Assert.Throws<SemaphoreFullException>(() => s.Release());
            }

            using (Semaphore s = new Semaphore(0, 10))
            {
                Assert.Throws<SemaphoreFullException>(() => s.Release(11));
                Assert.Throws<ArgumentOutOfRangeException>("releaseCount", () => s.Release(-1));
            }

            using (Semaphore s = new Semaphore(0, 10))
            {
                for (int i = 0; i < 10; i++)
                {
                    Assert.Equal(i, s.Release());
                }
            }
        }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:21,代码来源:SemaphoreTests.cs

示例14: 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

示例15: ReleaseMultipleBadArgument

 public void ReleaseMultipleBadArgument()
 {
     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


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