0

I am reading Exam Ref 70-483: Programming in C# by Wouter de Kort.
The writer doesn't explicitly mention the version of C#, but I guess it's 5.0 since he makes heavy use of async/await keywords.

The examples in this book only use Thread.Sleep() and not Task.Delay()

Parallel.For(0, 10, i =>
{
    Thread.Sleep(1000);
});

,

Task.Run(() =>
{
    bag.Add(42);
    Thread.Sleep(1000);
    bag.Add(21);
});

etc etc...

From other reading/SO questions like this, I'd figure that

await Task.Delay(1000)

should generally do better in a parallel context than

Thread.Sleep(1000)

because Task.Delay leaves it's thread unhindered thus allowing other tasks execute on it.
I've just Ctrl-F'd the book and it didn't find a single occurrence for Task.Delay! I'm confused between community opinions from the internet and official Microsoft book.
If Task.Delay is a good practice, why doesn't this book address it in any way?
Or did I miss something?

Community
  • 1
  • 1
evictednoise
  • 587
  • 1
  • 7
  • 19
  • 3
    Perhaps it would be better to ask the author of the book? – Martin Liversage Dec 07 '15 at 07:55
  • In order to use Task.Delay(1000) you have to use 'await ' which it forces your method to be async. Sometimes this is not a good approach. But you always can use Thread.Sleep() with no restrictions. – Mohammad Mirmostafa Dec 07 '15 at 07:59
  • `Task.Delay` is simply an awaitable version of `Thread.Sleep` for exactly the reasons you state, so the thread resource is freed to have other work scheduled to it. Simply, book code tends to be moderately good to follow but can drop the ball in the odd place. Review the book's errata online (if it has one), and review other sources such as articles or blog posts to get further opinions and explanations. No one book is the single source of truth for good programming practice and sometimes a book misses something or gets it wrong. – Adam Houldsworth Dec 07 '15 at 07:59

2 Answers2

1

The other question deals with real-world code; what you have in your book are code examples, which are quite different.

For code examples:

Thread.Sleep is appropriate if you want to block the current thread - i.e., you're simulating some synchronous / CPU-bound work.

Task.Delay is appropriate if you don't want to block the current thread - i.e., you're simulating some asynchronous / I/O-bound work.

For the particular examples you posted (code in Parallel.For and Task.Run), I'd say Thread.Sleep is the most appropriate. Parallel.For and Task.Run are specifically for running CPU-bound code on different threads, so a synchronous "placeholder" of Thread.Sleep is correct.

Note that in real-world code, any "placeholder" usages of Thread.Sleep and Task.Delay like this are replaced with real code.

In real-world code, Task.Delay is still useful for things like delayed retries. Thread.Sleep should not be in real-world code.

Community
  • 1
  • 1
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • Sir, thank you kindly for clearing it up for me. I misunderstood that Thread.Sleep is there to so simulate CPU-Bound work. Now it all makes sense. – evictednoise Dec 10 '15 at 07:26
0

Exam 70-483 is for Visual Studio 2012, when C# had no async/await.

Also, the Thread.Sleep() is just there to indicate work being done.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • async/await is explained and used in the book,as I wrote in the question. Also I think the exam is for language, not IDE. – evictednoise Dec 07 '15 at 08:09