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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

control in a loop 2

Status
Not open for further replies.

foxnet

Programmer
Mar 24, 2005
21
US
Hi,

I am trying to write a program that read files in a directory and move them to specific folders based on their extension. The files are sent over from another computer through ftp. I have a form that contain a command button and a check box. The check box is used to have it run continuously or one time only. In the click event of my dialog box I have the following code

thisform.get_filetype
do while thisform.chk_auto.value = .T.
thisform.get_filetype

enddo

The function get_filetype just uses an ADIR and moves the files. Everything is working fine but I have no way of breaking out of the loop. I was hoping I would still be able to click the chk_box and that will stop the loop. Is there a way to read window messages inside this loop ?

Thanks

 
What's the condition that determines when you should break out of the loop? Can't you test for it either before or after you call get_filetype?

do while thisform.chk_auto.value = .T.
IF endloopcondition
EXIT
ENDIF
thisform.get_filetype
enddo

Jim
 
Jim,

My condition was the value of the checkbox on the form. If the box is check the loop should run, when it is not checked it should break out

 
ADIR() returns the number of files found.

lnNum = ADIR()
FOR lnCount = 1 to lnNum
*copy stuff
NEXT lnCount

Regards,

Mike
 
Rather than have an infinite loop, which will hog all resources, add a pause somewhere to allow the UI to get a little CPU time.
One option would be to add a timer to your form with an interval of 1000 (1 second), which calls thisform.get_filetype when the timer event fires, rather than the do...while. That would allow you to uncheck the box during pause time.

-Dave Summers-
[cheers]
Even more Fox stuff at:
 
You can add this to your loop to allow it to be ended (interrupted) by pressing the escape key.

Code:
*___________________________________________________________________
*
*                     CANCEL OBJECT CLASS
*                        Version = 1.0
*             Copyright (C) 2002 Vignone Associates
*                      All rights reserved.
*___________________________________________________________________     
*
*  ABSTRACT.   
*     This Class contains allows easy cancellation of loops.
*
*  EXPORTS are...
*     Cancelled                  True when ESC is pressed
*     notCancelled               True while ESC not pressed
*
*  CONSTRAINTS are..
*     None.
*
*  USAGE is...
*     At the beginning of a loop...
*        oh = NEWOBJECT("Cancel")   Create a Cancel Object
*        on escape oh.Cancel        To activate the ESC key
*     On the DO include
*        DO while ... and oh.notCancelled
*     At the end of the loop...
*        oh.Destroy              Release the Cancel Object
*                             If cancelled, a message will appear. 
*___________________________________________________________________     
*  SAMPLE PROGRAM
*     clear
*     set escape off
*     oh = NEWOBJECT("Cancel")   && Create a Cancel object
*     on escape oh.Cancel 
*     i = 1
*     do while i < 10000 and oh.notCancelled
*        i = i+1
*        for j=1 to 99999  && waste some time
*        next j
*     enddo 
*     oh.Destroy
*___________________________________________________________________     
   
DEFINE CLASS Cancel AS CUSTOM
   Protected pEsc
   Cancelled = .F.
   notCancelled = .T.
   *==================== 
   Procedure Init
      this.pEsc = set("ESCAPE")
      set escape on
   EndProc
   *---------------------  
   Procedure Cancel
      local e,msg
      msg = "You pressed the ESC Key."+chr(13) ;
           +"Do you want to cancel the run?"
      if messagebox(msg,4+32,"Attention.") = 6 then
         this.Cancelled = .T.
         this.notCancelled = .F.
         e = this.pEsc
         set escape &e
         on escape
      endif
   EndProc
   *---------------------  
   Procedure Destroy
      local e
      e = this.pEsc
      set escape &e
      on escape
      if this.Cancelled then
         wait window nowait "    INTERRUPTED with ESC Key    "
      endif    
   EndProc
   *---------------------  
ENDDEFINE

Tony
 
Well, you don't really need a class. If you want to keep the loop and use the <Esc> key, this would probably work:
Code:
SET ESCAPE ON
ON ESCAPE thisform.chk_auto.value = .F.
do while thisform.chk_auto.value = .T.
    thisform.get_filetype
enddo
ON ESCAPE

-Dave Summers-
[cheers]
Even more Fox stuff at:
 
Dave,

What if someone lays a book on the ESC key? What happened to the previous setting of ON ESCAPE? What if you want all applications to use F10 instead of ESC?

Of course, if you don't believe in resuable code, that's cool.

Tony
 
What if someone lays a book on the ESC key
Uhh, well if someone lays a book on the keyboard during the instantiation of a messagebox, the messagebox will conitnue to pop up, go away, pop up. But nevertheless, what if someone spills coffee on the keyboard? What if the power goes out? What if...
Programmers are supposed to accommodate all of that. I take for granted settings are to be restored.

I believe in reusable code, but I was just showing an alternate method. Since, as always with VFP, there are a few different ones.

-Dave Summers-
[cheers]
Even more Fox stuff at:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top