Click here to Skip to main content
15,885,899 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

I want to know how can I add a delay (ex:200ms) to some received raw data before send it again through the network?

thanks in advance for your help. :-D :-D
Posted
Comments
SnowHow 10-Nov-10 9:42am    
I suppose you could always use Sleep(). There are alot of other options out there but that is really going to depend on what you are doing and why you want to hold on to that data for a little while..
fsidiosidi 10-Nov-10 10:03am    
I think sleep add a delay to treatment of data...but for my case I want to delay a queue of data...and still receiving data coming from the network....

1 solution

It really depends on what you are trying to achieve. What is the particular reason you want to do something like that ?
Do you have to add the same delay for all data received ?

One approach would be to create a kind of queue where you push data in and a separate thread pops data from the queue (using a kind of scheduling). This way, you delegate the wait to a separate thread in order not to block your processing thread.
 
Share this answer
 
Comments
fsidiosidi 10-Nov-10 10:22am    
Yes, exactly...I want to add the same delay for all the received data....It's kind of a delay simulator...I receive data-->add delay--->send again the data....so u said I should use 2 threads...ona for pushing into the queue and the second for poping from the queue?
Cedric Moonen 10-Nov-10 10:49am    
Well, you have your thread which is already receiving the data that will push the data in the queue, and you have one additional thread that is responsible for extracting data from the queue. What you could do is when you put data in the queue, you also keep the time at which it should be sent. Your thread that pops data from the queue will be notified via an event when data is pushed into the queue. At that time, it looks at the first data in the queue and wait for a certain period of time (until it reaches the specified time).

It is a bit complicated to explain but basically you have one event which is shared by the two threads: when data is pushed into the queue, the first thread signal the event to wake up the second thread, signaling it that data is available. This second thread, when woken up, will take the first data and look at the time at which it has to wake up. If the wait times out (meaning that no data has been pushed into the queue and that we reached the time at which we need to send data), the second thread takes the first data, sends it and looks if there's more data availble. If not, it wait for an "infinite" time on the event, otherwise it waits until the next data should be sent.
fsidiosidi 10-Nov-10 11:26am    
Thank you very much for explaining the process...but what if the rate by wich we receive data (let's say 64Kbit/s) is different from the rate by which we send it (1.5 Mbit/s)...here the poping thread is faster than the pushing thread which will cause a problem...don't u think so?
Cedric Moonen 10-Nov-10 13:18pm    
That's something different. What you asked for is be able to send data with a delay, not at a different rate. You can't send data faster than you receive it (and sending data slower than you receive it is not possible neither, since you will need to accumulate the data and at a certain point, your system will break). The popping thread isn't faster, it just pops data with a certain delay.
fsidiosidi 11-Nov-10 6:08am    
The problem is that I have to implement the both of them....can I just use the function sleep for the second thread in order to synchronize the receiving and sending rate...or maybe I can stop the thread, responsible for poping data from the buffer and sending it, for a while until I have enough data to send it... but this will cause another delay....by the way, is it possible to limit the rate of receiving or sending data over the network?...and thanks for your time Mr. Cedric

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900