Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4266326
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:44:43+00:00 2026-05-21T06:44:43+00:00

I am trying to test some classes that rely on a Task to do

  • 0

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:

private void Search()
{
    Task<SearchResults> searchTask = m_searchProvider.GetSearchTask(m_searchCriteria);
    searchTask.ContinueWith(OnSearchTaskCompleted);
    searchTask.Start();
}

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.

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.

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.

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:

public class NonThreadedAsync<TResult>
{
    private NonThreadedAsyncResult<TResult> m_asyncResult;

    public IAsyncResult BeginInvoke(
        AsyncCallback callback,
        object state)
    {
        m_asyncResult = new NonThreadedAsyncResult<TResult>(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<TResult> : 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<int> async = new NonThreadedAsync<int>();

        Task<int> task = Task<int>.Factory.FromAsync(async.BeginInvoke, async.EndInvoke, null);

        task.Start();
    }
}

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:

Id = 1, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"

but there is no Method property in the visible properties if I expand it.

Can anyone see what I’m doing wrong?

[Edit: I’ve also written a test that confirms the NonThreadedAsync class works correctly with the classic Begin/End pattern (or at least, my understanding of the Begin/End pattern :)) and this passes:

[TestMethod]
public void NonThreadedAsync_ClassicAccessPattern()
{
    int result = 0;
    bool asyncCompleted = false;

    NonThreadedAsync<int> async = new NonThreadedAsync<int>();

    async.BeginInvoke(asyncResult =>
                            {
                                result = async.EndInvoke(asyncResult);
                                asyncCompleted = true;
                            },
                            null);

    Assert.IsFalse(asyncCompleted);
    Assert.AreEqual(0, result);

    async.Complete(54);

    Assert.IsTrue(asyncCompleted);
    Assert.AreEqual(54, result);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T06:44:43+00:00Added an answer on May 21, 2026 at 6:44 am

    Oh, I get it. Our API was wrong in that it was trying to return non-started Tasks. Removing the Start() from inside the class under test solves the issue. In researching this, however, I have also found that I was doing far too much to get a test-controlled async Task. As per Stephen Toub’s post here, we can simply use a TaskCompletionSource:

    [TestMethod]
    public void TaskCompletionSource_WorksALotBetterThanMyOverEngineeredCustomStuff()
    {
        int result = 0;
    
        TaskCompletionSource<int> tcs = new TaskCompletionSource<int>();
    
        Task<int> myTask = tcs.Task;
    
        // Pretend this is the class under test and that we've
        // passed in myTask
        myTask.ContinueWith(t => { result = t.Result; },
            TaskContinuationOptions.ExecuteSynchronously);
    
        Assert.AreEqual(0, result);
    
        tcs.SetResult(54);
    
        Assert.AreEqual(54, result);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use PHPUnit to unit test some class methods that return SQL.
I am trying to develop a page to display student/class results by subject/teacher. Some
I'm struggling with some Generic constraint issues when trying to implement a library that
I'm trying to build an automatic testing framework (based on jUnit, but that's no
I have an application that I am working on modifying and I am having
our company is trying to increase software quality by enforcing minimum function coverage on
I'm not too familiar with using the <h:messages> element in JSF. What I'm trying
I have a bunch of Python modules I want to clean up, reorganize and
Please have a look at the followin code, where Extractor[A,B] is part of a
This is the directory structure of the project (maven2 is used): pom.xml /src /main

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.