10-05-2009 04:31 AM
hi,
I'm suffering inaccurate delay function when using ProcessSystemEvents. I have no need for 1 second accuracy, instead 10 seconds accuracy would be enough. But I need ProcessSystemEvents because I have need to wait quite a long time and doing it by only using Delay() locks the user interface. Below function I'm curretly using and e.g. 300 seconds delay will be 467 seconds without any user interface use. Is there any better solution to improve the accuracy using ProcessSystemEvents ? Or any other method to wait avoiding locking the user interface ?
Regards,
petri
void delay(double seconds)
{
long int max = 0;
long int index = 0;
max = (long int)seconds/0.1;
for (index=0;index<max;index++)
{
ProcessSystemEvents();
Delay(0.1);
}
}
Solved! Go to Solution.
10-05-2009 04:39 AM - edited 10-05-2009 04:47 AM
A long lasting delay loop can be implemented this way:
void LongDelay (double seconds)
{
double tini;
tini = Timer ();
while (Timer () - tini < seconds) {
ProcessSystemEvents ();
}
}
However, this is not the optimal solution if this function is called within a control callback, since callbacks should be designed to be as fat as possible.
One possible solution is to design a state machine built around a standard UI timer or better an asyncronous timer: this way the user interface level is free and your program can achieve a good performance (depending on the amount of operations in a loop, you may run the machine at 5 or 10 loops/sec). If you want to run this way, you may find in this document a tutorial on state machines; don't let all those LabVIEW citations frighten you (): state machine concepts are universal and you can develop fully functional machines in CVI as well as in LV.
10-05-2009 06:31 AM
thanks Roberto for quick answer. since for a while I programmed with CVI and forgot the Timer() function... That's of course with while more accurate and good enough at the moment.
regards,
petri
10-06-2009 05:26 PM
10-07-2009 01:48 AM
gtoph wrote:
Wouldn't a "syncwait" function call do what you need as well? I thought a process event's call was more or less done internally by this function as well.
Unfortunately not: syncwait is equivalente in this respect to Delay (); UI events are ignored during the wait, even UI timers are not honoured while waiting. Only functions executed in different threads remain alive (e.g. an async timer continues working).