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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:58:42+00:00 2026-06-15T03:58:42+00:00

Since Task-based Asynchronous Pattern is now the recommended route (per MSDN @ here and

  • 0

Since Task-based Asynchronous Pattern is now the recommended route (per MSDN @ here and here), how would I go about converting the simple hello world code below into a Task-based Asynchronous Pattern?

Lets assume I don’t know anything about Tasks and I’ve tried to demo input to and output from the worker as well as calling from the ‘main’.

class Program
{
    static void Main(string[] args)
    {
        Worker wk = new Worker();
        string result = wk.DoWork(1000);
        Console.WriteLine(result);
        Console.WriteLine("Main says, Hello World!");
        Console.ReadLine();
    }

}
class Worker
{
    public string DoWork(int delay)
    {
        Console.WriteLine("Worker says, working ...");
        Thread.Sleep(delay); // represents the 100ms+ workload            
        return "Worker says, I'm done! Hello World!";
    }
}
  • 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-15T03:58:43+00:00Added an answer on June 15, 2026 at 3:58 am

    This depends on what exactly do you want to make asynchronous. If the Thread.Sleep() represents some CPU-bound work, then you’re not going to gain much from using TAP.

    If you want to make the wait inside DoWork() asynchronous, that means changing the signature from string to async Task<string> and then using await Task.Delay() instead of Thread.Sleep().

    If you also want to make Main() asynchronous, you can, if you’re using C# 7.1 or newer.

    If you’re using an older version of C#, then it becomes. What you can do is to create asynchronous MainAsync() and then synchronously Wait() for that in the real Main(). This means the main thread would be blocked, but we kind of need that to keep the application alive. Also, mixing asynchronous await with synchronous Wait() is usually not a good idea (especially since it often leads to deadlocks), but in this case it’s okay.

    If we make Main() asynchronous, it would be nice to make the blocking ReadLine() asynchronous too. There is no Console.ReadLineAsync(), but we can use Console.In for that.

    So, the final code would look like this:

    class Program
    {
        static void Main()
        {
            MainAsync().Wait();
        }
    
        static async Task MainAsync()
        {
            var worker = new Worker();
            string result = await worker.DoWork(1000);
            Console.WriteLine(result);
            Console.WriteLine("Main says, Hello World!");
            await Console.In.ReadLineAsync();
        }
    }
    
    class Worker
    {
        public async Task<string> DoWork(int delay)
        {
            Console.WriteLine("Worker says, working ...");
            await Task.Delay(delay); // represents the 100ms+ workload
            return "Worker says, I'm done! Hello World!";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Since now I have only used plugin for editing and the way I use
Since Intellij does not yet support the Play-Scala-Template-Engine I was thinking about using plain
I have a task to optimize search engine in asp.net ecommerce store based on
The task I was trying to accomplish was that given an input pattern, e.g.
I am using a SessionWizardView and would like to get information about the currently
I'm currently trying to fill a fragment with an asynchronous task. It's mainly an
The task is to implement a Java Card application (Applet) with biomentric (fingerprint)-based security.
since memcpy should be highly optimized nowadays, does it still make sense to optimize
Since regular jQuery animations are not fluent on iOS ( .hide() , slideDown() ),
since today im facing a strange problem. When i start my app in my

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.