3

In my console application I have some threads and each thread have an infinite loop with a Thread.Sleep. Have many threads because each one is independent from others. Each cycle is run every waitingTime time and I want that never stops.

static int waitingTime= 5 * 60 * 1000;

static void Main(string[] args)
{
    Thread thread1 = new Thread(() => Thread1());
    Thread thread2 = new Thread(() => Thread2());
    Thread thread3 = new Thread(() => Thread3());
    ...
    thread1.Start();
    thread2.Start();
    thread3.Start();
    ...
}

static void Thread1()
{
      do
      {
          // Connect to DB and do something...
          try
          {
              using (SqlConnection connection = MyDBClass.MyDBConnection())
              {
                  connection.Open();
                  SqlCommand command = connection.CreateCommand();
                  command.CommandText = "SELECT x, y FROM Table WITH (NOLOCK);";
                  SqlDataReader sdr = command.ExecuteReader();
                  while (sdr.Read())
                  {
                      MyObject obj = new MyObject(sdr[0] as string, ...);
                      MyStaticClass.SetMyObject(obj); // Open a new DB connection on different server and do an Update.
                  }
                  sdr.Close();
                  sdr.Dispose();
              }
          }
          catch
          {
          }

          Thread.Sleep(waitingTime);
      }
      while (true);
 }

static void Thread2()
{
     do
     {
        // Connect to DB and do something...
        Thread.Sleep(waitingTime);
     }
     while (true);
 }

...

I have read that is not good use Thread.Sleep() in cases like this and is suposes use solutions like EventWaitHandle. What is the best solution for this case and how to implement that.

Sponsor
  • 368
  • 4
  • 9
  • 1
    Your use case isn't clear. Why do you need `Thread.Sleep` at all? Why do you think `EventWaitHandle` will help you? Please elaborate. – dymanoid May 29 '20 at 09:46
  • Are you using multiple threads to improve performance? Database access is better addressed through asynchrony rather than parallelism. – Johnathan Barclay May 29 '20 at 09:52
  • Use a timer and look into asynchronous programming. Creating threads andf then making them sleep is a waste of resources... – InBetween May 29 '20 at 10:00
  • @dymanoid ```Thread.Sleep``` is to makes the loop stop for waitingTime time. Using ```EventWaitHandle``` or ```AutoResetEvent``` is possible release CPU usage? – Sponsor May 29 '20 at 10:18
  • @JohnathanBarclay I use multiple threads to allow Thread1(), Thread2() ... code run independently from others. Can you tell me how is the best way to do that? – Sponsor May 29 '20 at 10:24
  • @Sponsor can you post your _Connect to DB and do something..._ code? – Johnathan Barclay May 29 '20 at 10:30

1 Answers1

1

A thread that is sleeping with Thread.Sleep() consumes zero CPU resources, so trying to improve on this using EventWaitHandle primitives or other means is pointless. You can't beat zero. What you could improve is the RAM consumed by the threads, which is 1 MB for their stack, plus the memory they allocate on the heap (which depends on what they do while working). So by using a single thread instead of three you could reduce the RAM consumption by 2 MB or more, which for a console app running on a modern PC is practically nothing. So I wouldn't be concerned about it in your place.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
  • Hi. There are a time where all my Threads are in Sleep and my CPU usage is about 10%. Sees you some reason for that? – Sponsor May 29 '20 at 11:45
  • @Sponsor they only thing I can think of is that the garbage collector is doing its job in its dedicated thread. It doesn't seem like a good explanation for a prolonged 10% CPU usage though. – Theodor Zoulias May 29 '20 at 11:52