Index of Scripts ---------------- 1: CodeWithCancel Script Name ( 1): CodeWithCancel -------------------------------------------------------------------------------- 'Date Created: 02-Mar-2009 09:41:11 AM 'Last Updated: 02-Mar-2009 09:56:34 AM 'Created By : Ira J. Perlow 'Updated By : Ira J. Perlow '========================================================================= ' Created by Computer Systems Design & Associates ' Copyright 2009 Computer Systems Design & Associates, All Rights Reserved ' http://www.csda1.com ' You may use this code at your own risk in whole or as a part. ' Use in full or part in operational code is permitted with proper ' attributions to the source, except it may not be sold as part of any ' software package that is a library of functions. Use in instructional ' materials is permitted if provided without modification. '========================================================================= ' This is sample code showing how to have a modeless dialog box to ' display periodic messages and and allow canceling code in a controlled ' manner ' Various samples of code or loops are shown. ' Set flag to false dim doneflag as L doneflag=.f. ' Set modeless dialog box title dim dialogtitle as c dialogtitle="Modeless Dialog Box" ' Set dialog body commands dim dialogbody as c dialogbody=<<%dlg% {can_exit=close} {text=50,2dialogmessage}; {Justify=center}{initial_focus}; %dlg% ' Set dialog body event handler dim dialogevent as c dialogevent=<<%code% IF a_dlg_button="Cancel Processing" a_dlg_button="" doneflag=.t. ui_modeless_dlg_close(dialogtitle) ELSE IF a_dlg_button="close" ' handle upper right red X of dialog box a_dlg_button="" ' You may want to ignore this, or process this differently 'doneflag=.t. 'ui_modeless_dlg_close(dialogtitle) END IF %code% ' Set initial dialog message dim dialogmessage as c dialogmessage="We are processing step 1" ' Start modeless dialog box ui_modeless_dlg_box(dialogtitle,dialogbody,dialogevent) '------------------------------------------------- ' do some code here statusbar.Set_Text("Step 1 Time: "+Time()) for i=1 to 5 ' Delay to simulate code sleep(1) next ui_yield() ' Yield to UI so we can process button if any xbasic_wait_for_idle() ' Wait for idle to give it time ' check for doneflag IF doneflag=.t. goto finish END IF '------------------------------------------------- ' Refresh modeless dialog box message if you want ' Message can be changed to indicate progress dialogmessage="We are processing step 2" ui_modeless_dlg_refresh(dialogtitle) ' do some code here statusbar.Set_Text("Step 2 Time: "+Time()) sleep(5) ' Delay to simulate code ' check for doneflag - Can be done as often as desired to exit code ' at points ui_yield() ' Yield to UI so we can process button if any xbasic_wait_for_idle() ' Wait for idle to give it time IF doneflag=.t. goto finish END IF '------------------------------------------------- ' Refresh modeless dialog box message if you want dialogmessage="We are processing step 3" ui_modeless_dlg_refresh(dialogtitle) ' do some more code here, here we display time for 5 seconds on message bar ' No check to exit, so we are stuck in loop for the whole time ' This is similar to long operations in Alpha 5 that is executed on 1 line ' e.g. query.create() for i=1 to 5 statusbar.Set_Text("Step 3 Time: "+Time()) sleep(1) next ' check for doneflag ui_yield() ' Yield to UI so we can process button if any xbasic_wait_for_idle() ' Wait for idle to give it time IF doneflag=.t. goto finish END IF '------------------------------------------------- ' Refresh modeless dialog box message if you want dialogmessage="We are processing step 4" ui_modeless_dlg_refresh(dialogtitle) ' do some more code here, here we display time for 15 seconds on status bar ' We check to exit for every interation, so we can exit at any time ' This is similar to code that you might loop on where you want ability ' to cancel on each loop. Cancel could be anywhere within the loop ' and could be checked multiple times at key points within loop for i=1 to 15 ' check for doneflag within loop ui_yield() ' Yield to UI so we can process button if any xbasic_wait_for_idle() ' Wait for idle to give it time IF doneflag=.t. goto finish END IF statusbar.Set_Text("Step 4 Time: "+Time()) sleep(1) next '------------------------------------------------- finish: ui_yield() ' Yield to UI so we can process button if any xbasic_wait_for_idle() ' Wait for idle to give it time ' Close modeless dialog box just in case it's left open IF ui_modeless_dlg_exist(dialogtitle)=.t. ui_modeless_dlg_close(dialogtitle) END IF ' cleanup code here, e.g. close tables and the like ' end script code end End Script ( 1)---------------------------------------------------------------