在.NET 4中使用计谋SemaphoreSlim.WaitAsync [英] Using await SemaphoreSlim.WaitAsync in .NET 4

查看:650
本文介绍了在.NET 4中使用计谋SemaphoreSlim.WaitAsync的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是使用.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中,因为加入这一方法<一HREF =htt​​p://msdn.microsoft.com/en-us/library/system.threading.semaphoreslim(v=vs.100).aspx相对=nofollow> SemaphoreSlim 在.net 4.5。

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

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

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

    公共任务WaitAsync()
    {
        锁定(m_waiters)
        {
            如果(m_currentCount大于0)
            {
                --m_currentCount;
                返回s_completed;
            }
            其他
            {
                VAR服务员=新TaskCompletionSource&LT;布尔&GT;();
                m_waiters.Enqueue(服务生);
                返回waiter.Task;
            }
        }
    }
    公共无效发行()
    {
        TaskCompletionSource&LT;布尔&GT; 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);
    }
}

这篇关于在.NET 4中使用计谋SemaphoreSlim.WaitAsync的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆