본문 바로가기

카테고리 없음

async lock

lock pattern을 사용할 때 제약 중 하나는, 그 안에 await을 사용할 수 없다는 것입니다.

그런데, WinRt에서 넘쳐나는 Async method들을 이용하다 보면 부득불 lock 을 사용해야 하는 경우가 생깁니다... :(

특히, 긴급하게 patch를 해야하는 경우!


가급적이면 await void 형태의 method 구조를 피하고, await Task 형태로 구현해서

해당 method 자체에 operation 이 끝나길 기다리는 게 좋겠지요.


정 필요한 경우에는 우리의 친구 NuGet을 이용하면 되겠습니다.

AsyncEx (#gitHub, #NuGet)


Supports

.NET 4.5/4.0, iOS, Android, Windows Store 8.0, Windows Phone Silverlight 8.0/7.5, Windows Phone Applications 8.1, Silverlight 5.0/4.0, and all portable libraries thereof.


Usage

Lock 패턴과 거의 같아요.

인자로 await AsyncLock.LockAsync() 가 들어가는 것만 빼면요.


References

http://stackoverflow.com/questions/7612602/why-cant-i-use-the-await-operator-within-the-body-of-a-lock-statement

http://stackoverflow.com/questions/12697178/how-to-handle-concurrency-with-storagefile-operations

https://github.com/StephenCleary/AsyncEx

http://www.nuget.org/packages/Nito.AsyncEx