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 8310333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:25:23+00:00 2026-06-08T19:25:23+00:00

I have been trying to get my head around C#’s new async/await and Task.Run

  • 0

I have been trying to get my head around C#’s new async/await and Task.Run functionality recently. In doing so I wrote some simple test code that writes to the console so that I can see in what order things happen in, throwing in a Thread.Sleep here and there to make sure things really happens in the order I expected.

Here is one of my tests

[Test]
public void TaskRun()
{
    Console.WriteLine("Before");
    Task.Run(() => Console.WriteLine(_terminator.IWillBeBack()));
    Console.WriteLine("After");
}

with the following Terminator class:

public class Terminator
{
    public string IWillBeBack()
    {
        return "I will be back";
    }
}

From this I expected the result to most likely be Before, After, and then I will be back. This is indeed what happens. Then I make a change to the IWillBeBack-method to allow it to sleep for 3 seconds. The code for this:

public class Terminator
{
    public string IWillBeBack()
    {
        Thread.Sleep(3000);
        return "I will be back";
    }
}

I expected the result to still be Before, After, and then after 3 seconds again I will be back. This is not what happens though. I instead get Before and After, but never I will be back.

Why is this?

When I debug it clearly goes through the code, sleeping and returning. I don’t need Thread.Sleep in this way in any production code, but I would like to have a vague understanding of why I will be back is not written to the console in the second case.

  • 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-06-08T19:25:25+00:00Added an answer on June 8, 2026 at 7:25 pm

    Your test is completing before it tries to print “I will be back”. You’re not doing anything to wait until that’s finished. Depending on the test runner, however it’s redirecting Console.WriteLine may have been disconnected as soon as the test has finished.

    If you want to see it finish, change your test to:

    [Test]
    public void TaskRun()
    {
        Console.WriteLine("Before");
        var task = Task.Run(() => Console.WriteLine(_terminator.IWillBeBack()));
        Console.WriteLine("After");
        task.Wait();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to get my head around some errors I get when
I have been trying to get my head around this but it hasn't been
I have been trying to get my head around to make things work with
I have been trying to get my head around how simplexml and dom updates
I'm new to web services in ASP.NET. Been trying to get my head around
I'm trying to get my head around Go + Appengine. I have been using
Been trying to get my head around while loops for the last few days
For sometime now, I have been trying to wrap my head around the reason
I'm trying to get my head around the idea of testing functionality and minimizing
I'm trying to get my head around solving the following problem. I have the

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.