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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:55:56+00:00 2026-06-18T11:55:56+00:00

I am using the Microsoft.Bcl.Async nuget package to use Async/Await (My target is .NET

  • 0

I am using the Microsoft.Bcl.Async nuget package to use Async/Await (My target is .NET 4, rather than 4.5)

I am new to Async/Await. My problem is described in the following code sample. Why is the highlighted line not called?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AsyncTest
{
class Program
{
    static void Main(string[] args)
    {
        Run();

        Console.WriteLine("Got the value");

        Console.ReadKey();
    }

    public static async void Run()
    {
        Console.WriteLine("Testing Async...");

        var val = await AsyncMethod();

        // HIGHLIGHTED LINE. WHY IS THIS LINE NOT CALLED?
        Console.WriteLine("And the value is... {0}", val);
    }

    public static Task<long> AsyncMethod()
    {
        long myVal = 1;

        for (long l = 0; l < 100000; l++)
        {
            myVal++;
            Console.WriteLine("l = {0}", l);
        }

        return new Task<long>(() => myVal);
    }
}
}
  • 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-18T11:55:58+00:00Added an answer on June 18, 2026 at 11:55 am

    The line:

    Console.WriteLine("And the value is... {0}", val);
    

    is converted (by the compiler) into a continuation; it will get invoked (with the value etc) only when the task that is being “awaited” completes. The problem is: you create a task, but you haven’t started it: so it will never complete.

    If you start the task you create at the bottom, it will eventually complete. Also, normally you would put the “doing” work inside the Task – not outside it:

        public static Task<long> AsyncMethod()
        {
            var task = new Task<long>(() =>
            {
                long myVal = 1;
    
                for (long l = 0; l < 100000; l++)
                {
                    myVal++;
                    Console.WriteLine("l = {0}", l);
                }
                return myVal;
            });
            task.Start();
            return task;
        }
    

    Also note that Got the value gets printed out long before the And the value is... – that is because the continuation happens on a worker thread; the Run method returns control to the caller (Main) at the first await.

    • 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 Nuget.Core to find Microsoft.Bcl.Immutable package (it is currently in prerelease
Using: Microsoft SQL Server 2008 Microsoft Visual Studio 2010 C# .NET 4.0 WinForms Ok
Microsoft introduced the IObservable<T> interface to the BCL with .NET Framework 4, and I
I'm using Microsoft Visual Studio 2008 with a Windows target deployment. How would I
I'm using Microsoft's new Team Foundation Service, http://tfs.visualstudio.com/ I wanted to know whether it
I am using Microsoft.SqlServer.Management.Smo . My Code: Server server = new Server(new ServerConnection( new
Hi I am using Microsoft Solver Foundation to solve Linear Programming problem, but the
I'm using Microsoft Speech API to load a grxml grammar: Grammar grammar = new
Using Microsoft SQL Server 2005 and above, what code do I use to validate
Using Microsoft Unity i register the following type: container.RegisterType(typeof(IRepository<>), typeof(NHibernateRepository<>)); In ASP.NET MVC 2

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.