I have a Windows Service that reads from multiple MessageQueue
instances. Those messagequeues all run their own Task
for reading messages. Normally
Using async-await
doesn't speed up the time it takes to execute a single operation, it just means that you don't have a thread waiting doing nothing.
In your case Task.Delay
will take a second no matter what but here:
Task.Delay(1000).Wait();
You have a thread that sits and waits for the second to end while here:
await Task.Delay(1000);
You don't. You are still asynchronously waiting (hence, await
) but no thread is being used which means better scalability.
In async-await
you get the performance boost because your app can do the same with less threads, or do more with the same threads. To measure that you need to have a lot of async
operations concurrently. Only then will you notice that the async
option utilizes CPU
resources better than the synchronous one.
More info about freeing threads here There Is No Thread
You're still running each task in its own thread from the thread pool - as you're using the default task scheduler. If you want to see performance imporvement, you'll need to make sure several tasks are performed on the same thread.
Also, with 20 parallel tasks, you're probably not going to see any difference. Try it with 2,000 tasks.