title: Non-blocking waits using TPL / C# 5 / .NET 4.0
body: <p>I have some common code I need to refactor back to C# 5 (my original question said C# 4) / .NET 4.0 so that I can share it among more projects.  The biggest impact is not using <code>async</code> / <code>await</code> and switching back to TPL and using <code>ContinueWith()</code>.</p>
  
  <p>Is there a way to accomplish non-blocking waits on synchronization objects without simulating this by throwing it into a background thread?  Timers I can use a <code>TaskCompletionSource</code> so I'm good there.</p>
  
  <pre><code>SemaphoreSlim mutex = new SemaphoreSlim(1);
  
  await mutex.WaitAsync(); // &lt;-- how to get a Task that completes on mutex.Wait()
  </code></pre>
  
  <p>e.g.</p>
  
  <pre><code>mutex.WaitInATask().ContinueWith(...
  </code></pre>
  
  <p><strong>UPDATE</strong> I have decided not to target C# 4.0 (but still target .NET 4.0 framework) because I'm more concerned about the assembly and not the source being able to be shared among projects.</p>
  
  <p>I have decided to go with using Microsoft.Bcl.Async, Stephen Cleary's AsyncEx, target .NET 4.0 framework, and set build C# version to 5.0 (to support async/await).  This would likely require VS2015 to compile but the assembly (along with dependencies) would target .NET 4.0.</p>
  
  <p>The additional benefit of this route is less code to refactor from <code>async</code>/<code>await</code> to <code>ContinueWith()</code> which should result in fewer bugs along the way...</p>