View Full Version here: : Delay function & Delphi
bojan
21-10-2010, 10:13 PM
The windows app for focus control via stepper motor is almost ready for posting.. apart from little problem: controlled delay around couple of milliseconds.
I have found a number of delay functions on the web (they are all polling RTC), but instead of milliseconds, they are exiting after seconds, sometimes longer..
The obvious alternative solution would be just a program loop, and it is good for me personally since I can adjust the delay to suit my machine..., but then it will not be good for anyone else... Does anyone know how to solve this properly?
All comments are most welcome and appreciated :)
AndrewJ
21-10-2010, 10:32 PM
Gday Bojan
If you want a synchronous function, just use sleep().
This works in milliseconds and is synched to the CPU clock automagically.
If you want an asynchronous method, use a TTimer.
You can program this in millisecs as well, and then just set it running as reqd
and create an event for when it stops, or you can test if its running and prevent a specific call going out till it stops.
Andrew
bojan
21-10-2010, 10:37 PM
Thanks mate, I will try.
I was also thinkig about this:
procedure TForm1.Delay(msecs:integer);
var
FirstTickCount:longint;
begin
FirstTickCount:=GetTickCount;
repeat
Application.ProcessMessages; {allowing access to other
controls, etc.}
until ((GetTickCount-FirstTickCount) >= Longint(msecs));
end;
Would omitting 'Application.ProcessMessages' help?
I don't mind if machine does nothing else while waiting, this app is for focus, therefore it will be used very occasionally.
mithrandir
21-10-2010, 10:49 PM
The traditional C method of doing a sleep with granularity less than 1 second is to use select() with the three fd_sets all being empty and the struct timeval being the required seconds and microseconds.
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
Note that sleep(1) is only guaranteed to sleep to the top of the next second, so not more than one second.
AndrewJ
21-10-2010, 10:51 PM
Gday Bojan
Just use the std sleep procedure then.
It automatically converts cpuTicks to millisec, hence is clock independent.
Much simpler than trying to roll yr own timer.
Andrew
PS the "process messages" wouldnt hurt in yr scenario, but will not do anything unless you have an event programmed for the message.
In the case you show, using a timer is probably a neater solution.
bojan
22-10-2010, 06:46 AM
Thanks guys,
sleep() actually works as expected (introducing delay couple of milliseconds long..
so I am sticking with this.
The app will be ready for distribution soon, after some more testing :-)
OneOfOne
22-10-2010, 08:05 AM
Always nice to see people using Delphi!
I had to do more than 5 years of c/c++ when I was in Telstra, but did all of my independent utilities in Delphi. Some VB, bit of Basic, Paradox and lots of Turbo Pascal. It always seemed to me that C can programs can be summarised in one word "obfiscation". My current job only uses Delphi (7) and the code base is at 360,000+ lines, it was 60,000 when I started 10 years ago and only a couple of thousand of those lines still exist.
bojan
22-10-2010, 08:48 AM
BTW, in case you didn't know (I didn't), Borland has released turbo Pascal 5.5 as freeware.
http://www.programmersheaven.com/download/6618/Download.aspx
Barrykgerdes
22-10-2010, 09:19 AM
Hi
I am interested in the methods of using "delay loops". I only program in basic (I am too old to learn something new) and the delay loops of course depend on computer speeds. Programs I wrote for the old PC's and XT's went haywire on 386's and 486's etc. So I needed a way to make delays constant.
When I needed to have a constant delay time I have always had to resort to reading the computer "ticks" over a set time period to produce a modifier to use in the delay loops. It works for me but is a bit messy because it takes a couple of seconds out of the start up time so I am always interested in other methods.
Barry
bojan
22-10-2010, 09:26 AM
I am also using program loops for delays/timing, when doing something in assembler (this is what I used for stepper driver for my EQ3 - very accurate, crystal controlled timing, as the execution lime for each instructions is known.. and processor (old one, Motorola KJ705) I am using does not have adequate timer).
However, those above-mentioned Delphi delays are funny... instead of milliseconds (what they are supposed to be), they are delaying things by seconds (or more.. I didn't figure out what is happening there). Even sleep(ms) is not quite accurate and it's longer (something to with multitasking in Windows?) but at least it is consistent and short enough to produce acceptable delays for stepper motor pulses duration.
OneOfOne
25-10-2010, 08:35 AM
Yes, we had to use this for a project last year, it had to run super fast under DOS on an old 486 laptop. It took me a while to remember some of the differences, but it fixed the problem, and much quicker to develop than machine code!
vBulletin® v3.8.7, Copyright ©2000-2025, vBulletin Solutions, Inc.