Page 1 of 4

Timers and loops

PostPosted: Thu Feb 04, 2010 4:33 pm
by Enrico Maria Giordano
Dear friends, is there any way to get a timer to count even if a loop without a sysrefresh() inside it is running?

EMG

Re: Timers and loops

PostPosted: Thu Feb 04, 2010 5:59 pm
by ukoenig
Enrico,

maybe a Solution :

Image

Code: Select all  Expand view

FUNCTION MAIN()

cTimeStart := Time()
i := 1
x := 0
FOR i := 1 to 12000000
    x++
NEXT
cTimeEnd := Time()

MsgAlert( "Start : " + cTimeStart + CRLF + ;
   "End : " + cTimeEnd + CRLF + ;
   CRLF + ;
   "Elapsed : " + TIME_DIFF( cTimeStart, cTimeEnd), "Elapsed Time ( 12000000-Counter )" )

RETURN NIL

// -----------------------------

FUNCTION TIME_COUNT(cTIME1,cTIME2)
local  nTIME1, nTIME2, nDELSECS, nHRS, nMINS, nSECS, nSECS1, nSECS2

nSECS1   := (val(substr(cTIME1,1,2)) * 3600) +;
              (val(substr(cTIME1,4,2)) * 60) + (val(substr(cTIME1,7)))
nSECS2   := (val(substr(cTIME2,1,2)) * 3600) +;
              (val(substr(cTIME2,4,2)) * 60) + (val(substr(cTIME2,7)))
nDELSECS := abs(nSECS2 - nSECS1)
nHRS     := int(nDELSECS / 3600)
nMINS    := int((nDELSECS - nHRS * 3600) / 60)
nSECS    := nDELSECS - (nHRS * 3600) - (nMINS * 60)

RETURN right("00" + ltrim(str(nHRS)),2) + ":" + ;
right("00" + ltrim(str(nMINS)),2) +  ":" + ;
right("00" + ltrim(str(nSECS)),2)
 


Best Regards
Uwe :lol:

Re: Timers and loops

PostPosted: Thu Feb 04, 2010 8:47 pm
by Enrico Maria Giordano
Sorry, I was referring to a Windows timer (FWH's TTimer class): it stops counting when the program is in a loop without a call to SysRefresh() inside it.

EMG

Re: Timers and loops

PostPosted: Thu Feb 04, 2010 10:23 pm
by James Bott
Enrico,

I believe everything stops when there is no sysrefresh() inside a loop (other apps also). That is why you should put sysrefresh() inside any loop that takes more than a couple of seconds.

James

Re: Timers and loops

PostPosted: Thu Feb 04, 2010 10:31 pm
by Enrico Maria Giordano
No, other apps don't stop. Maybe I can use threads but I'm not familiar with them.

EMG

Re: Timers and loops

PostPosted: Thu Feb 04, 2010 10:45 pm
by James Bott
Why don't you want to call sysrefresh()?

James

Re: Timers and loops

PostPosted: Thu Feb 04, 2010 11:06 pm
by Enrico Maria Giordano
Because I already have tons of loops without SysRefresh(). :-)

EMG

Re: Timers and loops

PostPosted: Thu Feb 04, 2010 11:21 pm
by James Bott
Enrico,

Well you say other apps are not affected, but that is not my experience. Trying running a continous loop without a sysrefresh():

do while .t.
endo

And then try running another app. It will be extremely sluggish since the loop is using 99% of the CPU time. Now try:

do while .t.
sysfresh()
enddo

And see the difference.

James

Re: Timers and loops

PostPosted: Fri Feb 05, 2010 7:49 am
by Enrico Maria Giordano
Sorry, I see no differences at all in my PC. Maybe because I have a dual processor PC?

EMG

Re: Timers and loops

PostPosted: Fri Feb 05, 2010 7:55 am
by Enrico Maria Giordano
Yes, it was that. If I run two empty loops then my system become sluggish too. Anyway, I don't really need to get the rest of the system responding fast during my waiting loops. I only need the timers going on with their work.

EMG

Re: Timers and loops

PostPosted: Fri Feb 05, 2010 8:56 am
by Antonio Linares
Enrico,

SysRefresh() lets Windows "breathe" and process its pending work :-)

If for some reason you can't call SysRefresh() then you may need to run your timer action yourself. In example, you can use GetTickCount() to count time and every amount of ticks, call yourself the timer action. That would be an equivalent for receiving and processing a timer event.

Re: Timers and loops

PostPosted: Fri Feb 05, 2010 9:06 am
by Enrico Maria Giordano
Ok, I realize that I can't do what I had in my mind. No problem.

EMG

Re: Timers and loops

PostPosted: Fri Feb 05, 2010 9:15 am
by Antonio Linares
Dear Enrico,

If you explain us what you have in your mind then we may try to help you :-)

Re: Timers and loops

PostPosted: Fri Feb 05, 2010 9:42 am
by Enrico Maria Giordano
Easy to explain: I have tons of loops without SysRefresh() that use a waiting message for the user, something like this:

Code: Select all  Expand view
MSGBOX( "Please wait..." )

WHILE ... // condition
    // do a lot of work
ENDDO

CLOSE MSGBOX


Now I would want to add something moving inside the function MsgBox() to give the user a better feel that the process is really going.

EMG

Re: Timers and loops

PostPosted: Fri Feb 05, 2010 10:50 am
by ukoenig
Enrico,

maybe have a look at < Progres1.prg, Progtime.prg and Win32 > ?

Best Regards
Uwe :lol: