Phil
Phil

Reputation: 349

Is this code synchronous or asynchronous?

Code below that I try to learn Task class. From the output, I see the main thread and task thread are running at the same time. But i get warning message in async method saying that:

"Warning 1 This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread."

so then is the code below synchronous?

namespace SampleThreadTaskClass
{
    class Program
    {
        static void Main(string[] args)
        {           
            Task task = new Task(ProcessDataAsync);
            task.Start();

            Console.WriteLine("Enter any key");

            string input = Console.ReadLine();
            Console.WriteLine("You entered: " + input);

            Console.ReadLine(); 
        }

        static async void ProcessDataAsync()
        {
            for (int i = 0; i < 20; i++)
            {
                Thread.Sleep(500);
                Console.WriteLine("processing... " + i);
            }
        }
    }
}

Upvotes: 4

Views: 224

Answers (3)

Alexander Sverla
Alexander Sverla

Reputation: 14

Yes it is. Thread.Sleep() is not asynchronous.

See Explanation: https://msdn.microsoft.com/en-us/library/hh156528.aspx#Example

Try delay instead, then you can add an await in front of it,

await Task.Delay(10000); 

Upvotes: -1

tRuEsAtM
tRuEsAtM

Reputation: 3668

What you should try is write some code before starting the task, then start the task in an asynchronous method. Then have another async method like ProcessDataAsync use await operator in the first async method. This way you can come to know about the async behavior.

This would help

static async Task ProcessDataAsync()
    {
        for (int i = 0; i < 20; i++)
        {
            await Task.Delay(500);
            Console.WriteLine("processing... " + i);
        }
    }

When you change your return type to async, you need to change your call toProcessDataAsyc to this

 var task = Task.Run(ProcessDataAsync);

And yes your code is async.

Upvotes: 0

Servy
Servy

Reputation: 203821

The ProcessDataAsync method is indeed synchronous. It claims to be asynchronous and it is lying in that claim.

You then provide that synchronous method to the constructor for Task which will execute that synchronous method in a thread pool thread (which you shouldn't use by the way; if you want to execute a synchronous method in a thread pool thread you should use Task.Run).

Of course, for your case you don't want to execute this method in a thread pool thread. You're asynchronous operation is just waiting. There's no need to schedule a new thread pool thread to just sit there doing nothing for seconds at a time.

You should make ProcessDataAsync actually be asynchronous, using Task.Delay to create a Task that will complete in a given interval of time, which you can await, and then you can simply call ProcessDataAsync when you want to start your asynchronous method and it will actually be asynchronous.

Upvotes: 1

Related Questions