The awaitawait keyword in C# (.NET Async CTP) is not allowed from within a locklock statement.
An await expression cannot be used in a synchronous function, in a query expression, in the catch or finally block of an exception handling statement, in the block of a lock statement, or in an unsafe context.
I assume this is either difficult or impossible for the compiler team to implement for some reason.
I attempted a work around with the using statement:
class Async { public static async Task<IDisposable> Lock(object obj) { while (!Monitor.TryEnter(obj)) await TaskEx.Yield(); return new ExitDisposable(obj); } private class ExitDisposable : IDisposable { private readonly object obj; public ExitDisposable(object obj) { this.obj = obj; } public void Dispose() { Monitor.Exit(this.obj); } } } // example usage using (await Async.Lock(padlock)) { await SomethingAsync(); } However this does not work as expected. The call to Monitor.ExitMonitor.Exit within ExitDisposable.DisposeExitDisposable.Dispose seems to block indefinitely (most of the time) causing deadlocks as other threads attempt to acquire the lock. II suspect the unreliability of my work around and the reason awaitawait statements are not allowed in locklock statement are somehow related.
Does anyone know why awaitawait isn't allowed within the body of a locklock statement?