Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Hello World" for Task-based Asynchronous Pattern?

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!";
    }
}
like image 942
DeepSpace101 Avatar asked Nov 27 '12 20:11

DeepSpace101


2 Answers

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!";
    }
}
like image 78
svick Avatar answered Oct 13 '22 11:10

svick


I understand that you want to execute DoWork() on a separate thread and have the result returned back. Although nonsensical, because threading wouldn't achieve anything in this example, below is the code for this.

class Program
{
    static void Main(string[] args)
    {
        Worker wk = new Worker();
        Task<string> task = Task.Factory.StartNew(() => wk.DoWork(1000));
        Console.WriteLine(task.Result);
        Console.WriteLine("Main says, Hello World!");
        Console.ReadLine();
    }

}

Task.Factory.StartNew() accepts a delegate and immediately starts executing it on a separate thread. It returns a Task object which represents the operation. task.Result will block the current thread until DoWork() has returned.

like image 21
Dave New Avatar answered Oct 13 '22 11:10

Dave New