StephenCleary / AsyncEx

A helper library for async/await.
MIT License
3.53k stars 356 forks source link

Cannot await 'Nito.AsyncEx.AwaitableDisposable<System.IDisposable>' #42

Closed mol closed 8 years ago

mol commented 8 years ago

I copy pasted the AsyncLock example and I'm getting an error message.

Cannot await 'Nito.AsyncEx.AwaitableDisposable'

The code is:

private readonly AsyncLock _mutex = new AsyncLock();
public async Task UseLockAsync()
{
    // AsyncLock can be locked asynchronously
    using (await _mutex.LockAsync())
    {
        // It's safe to await while the lock is held
        await Task.Delay(TimeSpan.FromSeconds(1));
    }
}
StephenCleary commented 8 years ago

What's the target platform for this code?

If you need an immediate solution, you can use SemaphoreSlim (built-in to newer versions of .NET). The syntax is a bit more awkward but it'll get the job done.

mol commented 8 years ago

Thanks for the fast reply :)

Framework is 4.5.1 and platform is x86. I'm awaiting other stuff, which works fine - just for some reason not this one.

I didn't know the SemaphoreSlim could handle the await inside, but I'll check that out, thanks.

StephenCleary commented 8 years ago

OK, SemaphoreSlim is available on that platform. It's just a bit more awkward because you have to use try/finally instead of using.

I'll look into this later tonight.

mol commented 8 years ago

I just tried the AsyncLock from here instead: http://blogs.msdn.com/b/pfxteam/archive/2012/02/12/10266988.aspx

I took everything exactly and not changing anything in your example, and it compiled. I'm using your NuGet package by the way - I don't know if that makes any difference. Strange. Anyway - thanks for looking into it :)

StephenCleary commented 8 years ago

Sorry, I've tried this with a few versions of VS, and I'm not able to repro this behavior.

It's possible that something got messed up during the NuGet install (or something weird happened like it was installed while the project was targeting .NET 4.0). If you could post a minimal repro, I can reopen this issue.

mol commented 8 years ago

That's alright, thanks for looking into it. I took your suggestion and used SemaphoreSlim instead, so until I need more complex async handling I'm good :) Thanks again.