Why is using System.Threading.Thread.Sleep () bad practice?

I am writing an application that works with a large and ugly third-party system through a complex API. Sometimes some errors occur in the system, but if we expect my program to encounter these errors, it may be too late.

So, I use a separate thread to check the status of the system as follows:

while (true) { ask_state(); check_state(); System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1)); } 

It doesn’t matter if I check the status of the system once every 100 ms or once a minute.

But I heard that using Thread.Sleep () is bad practice. What for? And what can I do in this situation?

+4
source share
3 answers

One reason is that Thread.Sleep() blocks your code from other actions. The last effort is to make the lock as small as possible. For example, node.js is a non-blocking language.

Update: I do not know about the infrastructure of the Timer class in C #. Perhaps this also blocks.

You can schedule a task to check this third API every 100 ms. Thus, within 100 ms your program can perform other tasks.

Update: This analogy may help. If we compare the operating system with the hospital and compare the threads with the nurses in this hospital, the supervisor (programmer) can choose the policy:

  • Or ask each nurse (thread) to observe her, and only one patient (work, task that must be completed), even if she waits an hour between each check ( Sleep() method)
  • Ask each nurse to check each patient and continue and check other patients during the interval until the next check.

The first model blocks. It does not scale. But in the second model, even with several nurses, you could serve many patients.

+5
source

Because the only way to disable this thread if it is waiting inside Sleep is to either a) wait for Sleep or b) use one of Thread.Abort or Thread.Interrupt . 1

If it is a long dream, then (a) is not suitable if you are trying to react. And (b) rather unpleasant if at that moment the code is not inside Sleep at that time.

This is much better if you want to be able to interrupt sleep behavior in an appropriate way, use the expected object (for example, ManualResetEvent ) - then you can place the expected object in the while conditional so that it makes it clear what will cause the stream to exit.


1 I use shutdown in this instance because it is a very common scenario that requires cross-threading. But for any other transmission or transmission over several channels, the same arguments can be used, and if this is not a shutdown, then Thread.Abort or Thread.Interrupt even less suitable.

+3
source

I would set a timer for any ms that you want, and wait for the completion of my verification methods, by the way, do you want to use an eternal loop or is this not the complete code that you showed there?

ok, this is a sample of what I'm talking about:

 public void myFunction() { int startCount = Environment.TickCount; ask_state(); check_state(); while (true) { if (Environment.TickCount - startCount >= 20000) //two seconds { break; } Application.DoEvents(); } } //Now you have an organized function that makes the task you want just call it every // time interval, again you can use a timer to do that for you private void timer_Tick(object sender, EventArgs e) { myFunction(); } 

luck

0
source

Source: https://habr.com/ru/post/899622/


All Articles