Delaying Program Execution in a Loop

  • Thread starter Thread starter Gregory A Greenman
  • Start date Start date
G

Gregory A Greenman

I have a program that accesses a web service. One of the terms of
use of this web service is that I am not supposed to call it more
than about once per second. My program currently runs much too
fast, so I need to add some sort of timer to bring it into
compliance with their terms. Here's what my program looks like
now:

Sub ProcessData
While MoreData
PrepareWebServiceRequest
SubmitRequest
ProcessWebServiceResponse
End While
End Sub

I need to change this so that SubmitRequest waits until a second
has elapsed since it last ran. Can someone tell me how I should
do this?

Thanks.
 
Gregory said:
I have a program that accesses a web service. One of the terms of
use of this web service is that I am not supposed to call it more
than about once per second. My program currently runs much too
fast, so I need to add some sort of timer to bring it into
compliance with their terms. Here's what my program looks like
now:

Sub ProcessData
While MoreData
PrepareWebServiceRequest
SubmitRequest
ProcessWebServiceResponse
End While
End Sub

I need to change this so that SubmitRequest waits until a second
has elapsed since it last ran. Can someone tell me how I should
do this?


I should add that I am using VB in VS.Net 2003.
 
If you are asking how to set up a timer event handler, then there should be
plenty of examples on the web. Usually though I would call the routine
because I need to, not because I can.

Is something in your client's state changing all the time that calling the
webservice is necessary this frequently? An example of this might be a model
that needs to report home that it is a gps readout and here is where I am....

Is something in the webservice state changing that frequently that you need
to call it so often? An example of this would be that the webservice is the
track of a FedEx package and you are trying to react to where to intercept
your neighbor's goodie bag...

One of those questions should answer how often to call it.
 
Thread.sleep

Very simple, note that this is about your main thread, as it is a
multithreading thread you have of course to use that.

http://msdn2.microsoft.com/en-us/library/system.threading.thread.sleep(VS.71).aspx


Ahh, thank you very much. I was aware of thread.sleep, but while
thinking about your answer, I see how thread.sleep can solve my
problem.

My thinking had been that if I use thread.sleep, I'd slow my
program down too much. If I sleep for 1.0 seconds, then the time
from one call to the next will be 1.0 seconds plus the time it
takes ProcessWebServiceResponse and PrepareWebSeviceRequest to
execute. Depending on where I am in my program, those two
procedures may take up a significant portion of the second by
themselves. So, I'd be waiting too long.

However, on further thought, I can record the tickcount when I
make the call. Then I use that and the current tickcount to
figure out how long to sleep. So my code will look like this:


dim intTimeOfLastCall as integer = 0

Sub ProcessData
While MoreData
PrepareWebServiceRequest
DelayCall
SubmitRequest
ProcessWebServiceResponse
End While
End Sub

Sub DelayCall
const DelayTime as integer = 1000

if environment.tickcount < intTimeOfLastCall + DelayTime then
thread.sleep(math.min(DelayTime, intTimeOfLastCall +
DelayTime - environment.tickcount))
endif

intTimeOfLastCall = environment.tickcount
End Sub

Thanks again. I was going around in circles trying to figure out
how to accomplish this and it turns out it was pretty easy.
 
Gregory,

Are you sure that your proces is not eating your processing time and then
you needs even more wait time?

Cor
 
I'm not sure what you're asking.

According to the terms of use of the web service I am accessing,
I need to average 1.0 seconds between calls. They don't care if
my program is doing something during that second or if it's just
sleeping.

Thanks,


Greg




Gregory,

Are you sure that your proces is not eating your processing time and then
you needs even more wait time?

Cor
 
Gregory,

I think it was me who did not understand your message, forget it?

Cor

Gregory A Greenman said:
I'm not sure what you're asking.

According to the terms of use of the web service I am accessing,
I need to average 1.0 seconds between calls. They don't care if
my program is doing something during that second or if it's just
sleeping.

Thanks,


Greg
 
Back
Top