2

I am currently trying to write some code which is going to send a request to a device, and then wait for the response. The problem is the response does not have an end character to indicate the message is complete so we just have to wait a period of time.

What I want to do is send the message and then wait a period of time. Each time a piece of data arrives I want to increment the period of time I am waiting by say another 100ms incase there is any more data to follow. If nothing comes in after that period it is assumed that the message is complete. This is the pseudo code I came up with:

var waitTime

Function SendData(data)
{
     send the request here and then block with the next code

     while (waitTime)
     {
          //wait
     }
}

Function RecieveData(data)
{
     Assign new data to a buffer
     waitTime++
}

The SendData and RecieveData functions are both on different threads within a class. I am coding this in C#. I have tried implementing this with a DataTime object, but it does not seem to be very accurate when working with 10's/100's of milliseconds?

Any advice on what would be the best way to implement this would be appreciated!

Regards Adrian

Adrian
  • 407
  • 6
  • 20

5 Answers5

2

Consider using the stopwatch class to measure small increments such as this.

JYelton
  • 35,664
  • 27
  • 132
  • 191
1

DateTime is accurate to approximately 1/64th of a second.

Why don't you just use System.Threading.Thread.Sleep(milliseconds)?

jason
  • 236,483
  • 35
  • 423
  • 525
  • As mentioned, I have tried implementing this with sleep, but the size of the data will vary, so when new data comes in we want to increment the wait time in case there is more data to follow. – Adrian Jan 13 '11 at 17:55
  • So just increase the sleep amount every time data arrives. Note that you can specify how long to sleep, and this is can be a variable, and you can modify that variable as needed. – jason Jan 14 '11 at 15:27
0

Try comparing DateTime.Ticks instead. A tick is 100 nanoseconds. It is as accurate as the system clock. Of course, the difference between two computers may be tremendous (comparitively).

Chris B. Behrens
  • 6,255
  • 8
  • 45
  • 71
  • 1
    The *accuracy* of the clock is irrelevant; what is relevant is the *precision*. It is precise to about 1/64 of a second. – Eric Lippert Jan 13 '11 at 17:48
  • Hi, thanks for the quick replies! I have tried implementing this using Thread.Sleep(), but I need to be able to increase the sleep time each time some new data comes in, just in case there is more data to follow. I guess I could Sleep, and then check if a boolean has been set to sleep a bit more? – Adrian Jan 13 '11 at 17:53
  • @Adrian: OK, so then increase the sleep time each time some new data comes in. Sleep takes as its parameter the length of time to sleep; increase the value of the parameter as you see fit. (Note that the time is *approximate*. The operating system gives you no guarantees that you will sleep for *exactly* that long.) – Eric Lippert Jan 13 '11 at 18:07
  • Alrighty, I am just going to use Thread.Sleep and use a global variable to increment the sleep time. After the initial sleep I will check if the sleep time has been increased by the DataIn method, if so It will sleep again. – Adrian Jan 13 '11 at 18:15
0

If you want to sleep for a while, just use Thread.Sleep(10000) this causes the thread to sleep for the number of miliseconds you want.

Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203
0

Ok, just to end this question I thought I would post my solution:

bool dataInFlag;

function sendData(data)
{
     Send();

     dataInFlag = true;

     while(dataInFlag)
     {
           dataInFlag = false;
           Thread.Sleep(400);
     }
}

function recieveData(data)
{
     Parse();
     dataInFlag = true;
}

So in a nutshell, the while loop will execute, set the dataInFlag back to false and then sleep, if no data is received during the sleep the while loop will exit. If data is received the dataInFlag is set back to true and the loop will fire again! I have extended this so that the first execution of the while loop sleeps for a longer period, and each subsequent loop sleeps for a shorter period.

Thanks again for all the replies.

Adrian
  • 407
  • 6
  • 20