Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • Nice solution, but I don't think it works for me. I can't use a Mutex because I need to await some tasks while I have acquired the semaphore, and so thread-affine locks are a no-go. Using Wait() or RunSynchronously() also is not an option. The StaTaskScheduler solution you proposed sounds good, but unfortunately, in the latest iteration of the WinRT APIs, the Thread class is nonexistant. Barring a hard reset of the system (which would clear the semaphore anyway), if I wrap the synchronized code in a try/finally, would there be any possibility of an abandoned semaphore? Commented Apr 17, 2014 at 13:28
  • @ZaneKaminski, if you carefully balance WaitOne/Release with try/finally everywhere, you should be fine with this approach. Commented Apr 17, 2014 at 22:11
  • @ZaneKaminski, also check my update regarding WinRT. Commented Apr 18, 2014 at 0:42
  • Great idea. I didn't even think about that Commented Apr 18, 2014 at 5:27