EdwinVW/pitstop

[Question] Why do you sleep the thread for 10 seconds in the message handler services?

davidhenley opened this issue · 2 comments

Just wondering if this is good practice for production or if this is something you are doing for something else. Why can't you just let it run?

Good question!

I have to keep the main thread running while the message-handler runs in the background on another thread. In Development mode, I can simply wait for a key-press. But in Production mode I'm running in a container and I cannot wait for a key-press because there's no console attached to it (it runs somewhere on a server). So I've used an endless loop using while(true) for this situation.

The Thread.Sleep in the body of the while block is there because an empty wait-loop will be looping really fast. And although the body has no code, this will consume CPU cycles.

This is the output of the process in the task manager for an empty loop:

image

and this is the output for a loop with a Thread.Sleep(10000) in it:

image

As you can see, the loop with the sleep consumes 0% CPU. Only once every 10 seconds you may see a small spike (of max 1%).

I always use this way of waiting on background threads when there's no console. Maybe there's another (more elegant) way, but this has always worked fine for me.

Does this make sense and answer you question?

Yes thank you!