在.NET 4中使用计谋SemaphoreSlim.WaitAsync计谋、NET、WaitAsync、SemaphoreSlim

由网友(抹不掉的痛又有谁会懂)分享简介:我的应用程序是使用.NET 4我使用计谋异步使用的NuGet包 在我的应用程序,我想要做一个等待关于sempahore WaitAsync电话如下:SemaphoreSlim semphore =新SemaphoreSlim(100);等待semphore.WaitAsync();不过我收到以下编译错误。System...

我的应用程序是使用.NET 4我使用计谋异步使用的NuGet包

在我的应用程序,我想要做一个等待关于sempahore WaitAsync电话如下:

  SemaphoreSlim semphore =新SemaphoreSlim(100);
等待semphore.WaitAsync();
 

不过我收到以下编译错误。

       

System.Threading.SemaphoreSlim不包含WaitAsync的定义,并没有扩展方法'WaitAsync接受第一种类型System.Threading.SemaphoreSlim的说法可以找到(是否缺少using指令或程序集引用?)

  

莫不是反正在.NET 4.0中uisng WaitAsync的?

解决方案 解密神奇的新媒体 黄埔军校 为何屡屡培养出新媒体老板级人物

您无法使用 SemaphoreSlim.WaitAsync 在.NET 4.0中,因为加入这一方法 SemaphoreSlim 在.net 4.5。

不过,您可以实现自己的 AsyncSemaphore 以下斯蒂芬Toub在的大厦异步协调原语,第5部分:AsyncSemaphore :

 公共类AsyncSemaphore
{
    私人只读静态任务s_completed = Task.FromResult(真正的);
    私人只读队列< TaskCompletionSource<布尔>> m_waiters =新问答LT; TaskCompletionSource<布尔>>();
    私人诠释m_currentCount;

    公共AsyncSemaphore(INT initialCount)
    {
        如果(initialCount℃下)抛出新ArgumentOutOfRangeException(initialCount);
        m_currentCount = initialCount;
    }

    公共任务WaitAsync()
    {
        锁定(m_waiters)
        {
            如果(m_currentCount大于0)
            {
                --m_currentCount;
                返回s_completed;
            }
            其他
            {
                VAR服务员=新TaskCompletionSource<布尔>();
                m_waiters.Enqueue(服务生);
                返回waiter.Task;
            }
        }
    }
    公共无效发行()
    {
        TaskCompletionSource<布尔> toRelease = NULL;
        锁定(m_waiters)
        {
            如果(m_waiters.Count大于0)
                toRelease = m_waiters.Dequeue();
            其他
                ++ m_currentCount;
        }
        如果(toRelease!= NULL)
            toRelease.SetResult(真正的);
    }
}
 

My application is using .NET 4. I am using await async using nuget package

In my application I want to do an await on sempahore WaitAsync call as follows.

SemaphoreSlim semphore = new SemaphoreSlim(100);
await semphore.WaitAsync();

However I am getting following compilation error.

'System.Threading.SemaphoreSlim' does not contain a definition for 'WaitAsync' and no extension method 'WaitAsync' accepting a first argument of type 'System.Threading.SemaphoreSlim' could be found (are you missing a using directive or an assembly reference?)

Could there be anyway of uisng WaitAsync in .NET 4.0?

解决方案

You can't use SemaphoreSlim.WaitAsync in .Net 4.0 since this method was added to SemaphoreSlim in .Net 4.5.

You can however implement your own AsyncSemaphore following Stephen Toub's example in Building Async Coordination Primitives, Part 5: AsyncSemaphore:

public class AsyncSemaphore
{
    private readonly static Task s_completed = Task.FromResult(true);
    private readonly Queue<TaskCompletionSource<bool>> m_waiters = new Queue<TaskCompletionSource<bool>>();
    private int m_currentCount; 

    public AsyncSemaphore(int initialCount)
    {
        if (initialCount < 0) throw new ArgumentOutOfRangeException("initialCount");
        m_currentCount = initialCount;
    }

    public Task WaitAsync()
    {
        lock (m_waiters)
        {
            if (m_currentCount > 0)
            {
                --m_currentCount;
                return s_completed;
            }
            else
            {
                var waiter = new TaskCompletionSource<bool>();
                m_waiters.Enqueue(waiter);
                return waiter.Task;
            }
        }
    }
    public void Release()
    {
        TaskCompletionSource<bool> toRelease = null;
        lock (m_waiters)
        {
            if (m_waiters.Count > 0)
                toRelease = m_waiters.Dequeue();
            else
                ++m_currentCount;
        }
        if (toRelease != null)
            toRelease.SetResult(true);
    }
}

阅读全文

相关推荐

最新文章