title: When wrapping traditional asynchronous handlers to TPL Task<T> what happens to the Callback and State?
body: <p><a href="http://msdn.microsoft.com/en-us/library/dd997423.aspx" rel="nofollow">This MSDN page has the following example</a>:  The intent is to wrap an APM style task that can't be represented in the various <code>Func&lt;T1, T2, T3&gt;</code> overloads.</p>
  
  <pre><code>static Task&lt;String&gt; ReturnTaskFromAsyncResult()
  {
      IAsyncResult ar = DoSomethingAsynchronously();     // &lt;-- If this is an APM method, it's a bad example since most APM methods have a parameter for callback and state.
      Task&lt;String&gt; t = Task&lt;string&gt;.Factory.FromAsync(ar, _ =&gt;
          {
              return (string)ar.AsyncState;
          });
  
      return t;
  }
  </code></pre>
  
  <p>My question has to do with the function <code>DoSomethingAsynchronously();</code>.  Most APM functions I've seen require the parameter callback and state, which is missing in this sample.</p>
  
  <p><strong>QUESTION:</strong>  What happens to the callback and state parameters in "DoSomethingAsynchronously"</p>
  
  <p>What do I need to do to properly call a function similar to this?  In my case I'm trying to wrap Azure Table calls like this</p>
  
  <pre><code>    Task CreateAsync(CloudTable tbl, CancellationToken token, object state)
      {
          ICancellableAsyncResult result = tbl.BeginCreate(null, state);  // Incorrect
          token.Register((o) =&gt; result.Cancel(), state);
  
          Task&lt;bool&gt; t = Task.Factory.FromAsync(result, _ =&gt;
          {
              return (bool)result.AsyncState;
          });
  
          return t;
      }
      Task&lt;bool&gt; ExistsAsync(CloudTable tbl, CancellationToken token, object state)
      {
          ICancellableAsyncResult result = tbl.BeginExists(null, state);  // Incorrect
          token.Register((o) =&gt; result.Cancel(), state);
  
          Task&lt;bool&gt;  t = Task.Factory.FromAsync(result, _ =&gt;
          {
              return (bool)result.AsyncState;
          });
  
          return t;
      }
  </code></pre>