Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use of Timer control 1

Status
Not open for further replies.

SitesMasstec

Technical User
Sep 26, 2010
495
2
18
BR
Dear colleagues:

The code bellow (intended to create a phantom effect - a text diving from above to bellow) is in the Click event of a Command button:

Code:
thisform.MyText.Caption="Hello Word!"

FOR Position=200 TO 380 SKIP 10
	thisform.MyText.Top = Position
	thisform.MyText.Visible = .T.

	FOR MyCounter=1 TO 80000     && Just count to 80000 before 
	NEXT MyCounter               && cleaning MyText on the form 

	thisform.MyText.Visible = .F.
NEXT Position
thisform.MyText.Visible= .F.

It works fine (in my computer, a 2.61Ghz processor). But I think that is a processor with a greater speed it will not show the effect.

I know that I may (or must?) use the Timer control instead of the FOR...NEXT (used in the code above), but ... how?


Thank you,
SitesMasstec
 
You could use a timer, but in this case, the API Sleep function would be easier.

Do this once:
Code:
DECLARE Sleep IN kernel32 INTEGER dwMilliseconds

And then do this inside your loop:
Code:
Sleep(1000)  && pauses for 1 second, adjust to what you need

Tamar
 
Hello Tamar!

Thank you, and nice to hear from you!

I used all the code you provided inside the the Click event of a Command button:

Code:
DECLARE Sleep IN kernel32 INTEGER dwMilliseconds
or must I put the above in the Init or PRG (which calls the form) ??


I use Sleep(3) to have the desired effect (the same obtained using just in my computer):
Code:
	FOR MyCounter=1 TO 80000     && Just count to 80000 before 
	NEXT MyCounter               && cleaning MyText on the form

So, instead of my code (FOR...NEXT) the Sleep will provide the same amount of time in any computer, which is what I want.

This kernel32 will work in any Windows (32 or 64 bits)?



Thank you,
SitesMasstec
 
>This kernel32 will work in any Windows (32 or 64 bits)?
Yes
kernel32_gx81vv.png


In the moment the Windows system will not have a 32bit subsystem, VFP applications themselves won't run.

You still haven't got it, do you? Any software not recompiled as 64bit software still does run, it does so, because Microsoft kept a 32bit subsystem and made a few mechanisms like split system folders, split registry and more.

Bye, Olaf.
 
Don't put the DECLARE in the Cllick event. Although it does no serious harm, it is wasteful of resources. Better to put it at the very start of your program, where you do all your other initialisation.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hello colleagues!

Ok, Mike, I followed yor advice and put the DECLARE clause in the main PRG file (which calls the form), thank you.

Olaf, thank you for your clear explanation about 32 bit running on a 64bit machine. Now I got it.

Tamar, I implemented your solution, thanks again. Just a note: if I use SLEEP(1) or SLEEP(5) or SLEEP(10) the result is the same!


Thank you,
SitesMasstec
 
if I use SLEEP(1) or SLEEP(5) or SLEEP(10) the result is the same

That shouldn't be the case. But it's possible that the numbers are so small that you don't notice the difference. Remember, those numbers are milliseconds. If you want the text to be visible long enough for the user to see it, you would need to give it at least one second, and possibly more. So SLEEP(1000) would be more appropriate.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 

Oh, yes Mike, I understand this. I used SLEEP(3) because the code bellow (using SLEEP(500)) took 100 seconds to show the "phantom" message on the form:

Code:
thisform.MyText.Caption="Hello Word!"

FOR Position=200 TO 380 SKIP 10
	thisform.MyText.Top = Position
	thisform.MyText.Visible = .T.

	SLEEP (500)

	thisform.MyText.Visible = .F.
NEXT Position
thisform.MyText.Visible= .F.

If SLEEP (500) takes 0,5 second...
... the loop 200 TO 380 SKIP 10 (200, 210, 220,... till 380) is equal to 19 times x 0,5 second = (shouLd take) 9,25 seconds, but it took more than a minute and a half (100 seconds)!!



Thank you,
SitesMasstec
 
FOR has STEP, not SKIP.

SKIP was most likely ignored, so you steppd through 181 iterations.

Just tried this, it doesn't error, but it steps with step size 1, just as the default FOR loop does:
Code:
FOR i = 10 TO 100 skip 10
  ? i
ENDFOR

You want to loop in increment steps of 10:
Code:
FOR i = 10 TO 100 [highlight #FCE94F]STEP[/highlight] 10
  ? i
ENDFOR

Bye, Olaf.
 
Do you have a good reason for toggling MyText.Visible each time round the loop, that is, just before and after calling SLEEP()? It seems to me that that would cause an unpleasant flicker. I wonder what the effect would be if you took that out - in other words, only set .Visible to .T. before the start of the loop and then to .F. at the end.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike, there might be a good reason for that in conjunction with Windows Fonts ClearType technique, making a text invisible might clear itself and all extra ClearType pixels, while simply just moving it might keep some blurred pixels.

Haven't tried, though.

Bye, Olaf.
 

One problem I detected is that it is not obeying the SKIP 10. The text is going down stopping each point during 0,5 second (Top=200, Top=201, Top=202... till Top=380)!


Thank you,
SitesMasstec
 
Yes, that should be clear now, because SKIP is the wrong option for a FOR loop, see my answer above.

Bye, Olaf.
 

Yes Olaf, I changed the wrong clause SKIP to STEP and now it is ok!

Mike, yes, really MyText.Visible is not needed each time round the loop. I put it before the loop.

Now the problem is solved with your valuable help. Thank you all!



Thank you,
SitesMasstec
 
Yes, that would explain it. The SKIP is ignored - or, rather, it is treated as a comment. This is the case with all (or, at least, most) of this type of construct. It's why you can write something like [tt]IF <condition> / <commands> / ENDIF <condition>.[/tt]

That's why it is taking so long. It is exectuting the loop 180 times rather than 18 times.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Exactly, Mike. I tried (see above) and SKIP 10 doesn't error but means the default step size of 1.

Bye, Olaf.
 
The "kindness" of the parser works in your favor some times, such as the days you've been working a lot in VBScript:

Code:
If <condition> Then

Other times, it just hides glaring errors. [wink]
 
Well, THEN actually IS an optional keyword and not just "kindness".

In the end, it's planned kindness to accept THEN instead of the usual simple quirk of the compiler to not care about anything following an END keyword.

The parser kindly skipping the SKIP also isn't the usual type of carelessly ignoring anything after the ENDxyz part of VFP code.

Seems the parser only cares for STEP after the upper limit of a for loop, although you can write something like
Code:
additional=3
FOR i = 1 TO 10 + additional
  ? i
ENDFOR
Where the [tt]+ additional[/tt] part makes sense and is also handled correctly. So it's not simply explained by the parser ignoring the rest of the line unless there is a STEP keyword, the parser has to expect any expression as the upper loop limit. So it's yet another quirk of the parser and compiler.

There are a few more, I think every command or part of command having no further extension means the parser simply ignores the rest of the line. You can also write anything after DOEVENTS FORCE

SKIP is tricky in this context, as it's a reserved word and command itself, it's also syntax colored blue (with standard syntax colors), so it also doesn't get apparent, but debugging shows your step increments are just 1.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top