-2

I'm developing a c# application that periodically sends message to a device.

First question : What is the best way to run a function that sends message periodically (every 1 second)?

Second question : Sometimes I want to stop periodic request, run a block of code, and start periodic request again. What is the best way to implement this mechanism?

It's clear that periodic request should run in another thread.

Before running "a block of code", stopping periodic request and starting again at the end of code block might be a solution but I want to implement something smart on periodic request side.

apxcode
  • 7,696
  • 7
  • 30
  • 41
penguru
  • 4,342
  • 11
  • 46
  • 56

3 Answers3

0

First question: You want a thread. Inside the thread you want an infinite loop. Then an if statement checking a timer.

Second question: Then you want a semaphore inside that loop. The semaphore will block the the code for you and then awaken it once you are ready.


   while(true)
   {
      check semaphore

      if one second has passed 
         then send message.
   }
apxcode
  • 7,696
  • 7
  • 30
  • 41
0

The Threading Timer is what you should look at

Scordo
  • 1,041
  • 7
  • 12