title: How to create a Task with TaskFactory.FromAsync and custom Async procedures
body: <p>I am trying to test some classes that rely on a Task to do some background computation (retrieve data from a network location).  The class gets a non-started instance of a Task, adds a ContinueWith method and then calls Start on the Task.  Something like this:</p>
  
  <pre><code>private void Search()
  {
      Task&lt;SearchResults&gt; searchTask = m_searchProvider.GetSearchTask(m_searchCriteria);
      searchTask.ContinueWith(OnSearchTaskCompleted);
      searchTask.Start();
  }
  </code></pre>
  
  <p>The class gets hold of the instance of the Task through an interface, so I am able to inject a Task instance that my test is in control of.  I can't seem to create one that I have enough control over, however.</p>
  
  <p>I don't want to introduce threading into the test, but still want to make the Task behave asynchronously, so what I have tried to do is write a class that implements the BeginInvoke/EndInvoke pattern without threading, and use the TaskFactory.FromAsync method to create the Task.</p>
  
  <p>The idea being that the test can invoke the method on the class that starts the task, and then when that returns the test can supply the result data to the Async object, which finishes the operation whilst remaining on the same thread.</p>
  
  <p>However, when I try and call Start on that Task, I get an error stating that "Start may not be called on a task with null action."  Google doesn't much help me on that message, unfortunately, so I'm unsure whether I've implemented my Async object incorrectly, or am using the TaskFactory.FromAsync wrongly.  Here's my code for my NonThreadedAsync classes and a test that blows up with the exception:</p>
  
  <pre><code>public class NonThreadedAsync&lt;TResult&gt;
  {
      private NonThreadedAsyncResult&lt;TResult&gt; m_asyncResult;
  
      public IAsyncResult BeginInvoke(
          AsyncCallback callback,
          object state)
      {
          m_asyncResult = new NonThreadedAsyncResult&lt;TResult&gt;(callback, state);
          return m_asyncResult;
      }
  
      public TResult EndInvoke(IAsyncResult asyncResult)
      {
          return m_asyncResult.GetResults();
      }
  
      public void Complete(TResult data)
      {
          m_asyncResult.CompleteAsync(data);
      }
  }
  
  public class NonThreadedAsyncResult&lt;TResult&gt; : IAsyncResult
  {
      private readonly AsyncCallback m_asyncCallback;
      private readonly object m_state;
      private readonly ManualResetEvent m_waitHandle;
      private bool m_isCompleted;
      private TResult m_resultData;
  
      public NonThreadedAsyncResult(AsyncCallback asyncCallback, object state)
      {
          m_asyncCallback = asyncCallback;
          m_state = state;
          m_waitHandle = new ManualResetEvent(false);
          m_isCompleted = false;
      }
  
      public void CompleteAsync(TResult data)
      {
          m_resultData = data;
          m_isCompleted = true;
          m_waitHandle.Set();
          if (m_asyncCallback != null)
          {
              m_asyncCallback(this);
          }
      }
  
      public TResult GetResults()
      {
          if (!m_isCompleted)
          {
              m_waitHandle.WaitOne();
          }
          return m_resultData;
      }
  
      #region Implementation of IAsyncResult
  
      public bool IsCompleted
      {
          get { return m_isCompleted; }
      }
  
      public WaitHandle AsyncWaitHandle
      {
          get { return m_waitHandle; }
      }
  
      public object AsyncState
      {
          get { return m_state; }
      }
  
      public bool CompletedSynchronously
      {
          get { return false; }
      }
  
      #endregion
  }
  
  [TestClass]
  public class NonThreadedAsyncTests
  {
      [TestMethod]
      public void TaskFactoryFromAsync_CanStartReturnedTask()
      {
          NonThreadedAsync&lt;int&gt; async = new NonThreadedAsync&lt;int&gt;();
  
          Task&lt;int&gt; task = Task&lt;int&gt;.Factory.FromAsync(async.BeginInvoke, async.EndInvoke, null);
  
          task.Start();
      }
  }
  </code></pre>
  
  <p>As further information, if I debug that test, just before it calls Start(), the task instance is showing up in the Locals window like this:</p>
  
  <p><code>Id = 1, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"</code></p>
  
  <p>but there is no Method property in the visible properties if I expand it.</p>
  
  <p>Can anyone see what I'm doing wrong?</p>
  
  <p>[Edit: I've also written a test that confirms the NonThreadedAsync class works correctly with the classic Begin/End pattern (or at least, <em>my</em> understanding of the Begin/End pattern :)) and this passes:</p>
  
  <pre><code>[TestMethod]
  public void NonThreadedAsync_ClassicAccessPattern()
  {
      int result = 0;
      bool asyncCompleted = false;
  
      NonThreadedAsync&lt;int&gt; async = new NonThreadedAsync&lt;int&gt;();
  
      async.BeginInvoke(asyncResult =&gt;
                              {
                                  result = async.EndInvoke(asyncResult);
                                  asyncCompleted = true;
                              },
                              null);
  
      Assert.IsFalse(asyncCompleted);
      Assert.AreEqual(0, result);
  
      async.Complete(54);
  
      Assert.IsTrue(asyncCompleted);
      Assert.AreEqual(54, result);
  }
  </code></pre>