title: how to implement async method using Task Parallel Library for I/O operations
body: <p>I found that for expensive IO bound operation I can use <strong>TaskCompletionSource</strong></p>
  
  <p>as shown here <a href="http://msdn.microsoft.com/en-us/library/hh873177.aspx#workloads" rel="nofollow">http://msdn.microsoft.com/en-us/library/hh873177.aspx#workloads</a></p>
  
  <p>But the example shown is only waiting for some time and return DateTime.</p>
  
  <pre><code>public static Task&lt;DateTimeOffset&gt; Delay(int millisecondsTimeout)
  {
   TaskCompletionSource&lt;DateTimeOffset&gt; tcs = null;
   Timer timer = null;
  
   timer = new Timer(delegate
   {
      timer.Dispose();
      tcs.TrySetResult(DateTimeOffset.UtcNow);
   }, null, Timeout.Infinite, Timeout.Infinite);
  
   tcs = new TaskCompletionSource&lt;DateTimeOffset&gt;(timer);
   timer.Change(millisecondsTimeout, Timeout.Infinite);
   return tcs.Task;
  }
  </code></pre>
  
  <p>Above code waits for timeout. I have a database call  which I want to fire in the above way, but little confused in how to write it:</p>
  
  <pre><code>   using (var context = new srdb_sr2_context())
     {
        return context.GetData("100", "a2acfid");
     }
  </code></pre>
  
  <p>I wrote the function as below, but not sure if this is <strong>correct way of doing it</strong>:</p>
  
  <pre><code>TaskCompletionSource&lt;IList&lt;InstructorsOut&gt;&gt; tcs = null;
          Timer timer = null;
  
          timer = new Timer(delegate
          {
              timer.Dispose();
  
              //prepare for expensive data call
              using (var context = new srdb_sr2_context())
              {
                  var output = context.GetData("100", "a2acfid");
  
                  //set the result
                  tcs.TrySetResult(output);
              }  
  
          }, null, Timeout.Infinite, Timeout.Infinite);
  
          tcs = new TaskCompletionSource&lt;IList&lt;InstructorsOut&gt;&gt;(timer);
          timer.Change(0, Timeout.Infinite);
          return tcs.Task;
  </code></pre>
  
  <p>Any help would be appreciated.</p>