Page 1 of 1

pause and resume execution

Posted: Mon Oct 07, 2024 6:22 am
by RPC
Hi
I want to download many files from a website. The website doesn't allow to download all the files at a stretch. I have to therefore pause the access and download for some time , say 2 minutes, and then resume download, During the pause time I want a stopwatch to be displayed at STATUSBAR.
There is a MessageBoxTimeOut function which can achieve this but it has its own window displayed. I do not want that. Is there any way to achieve this ?
Many Thanks for any help.
Rajeev.

Re: pause and resume execution

Posted: Mon Oct 07, 2024 10:03 am
by edk
Maybe something like this?:

Code: Select all

#include "hmg.ch"

Function Main

Public lPublicForceResume := .F.

	DEFINE WINDOW Form_1 ;
		AT 0,0 ;
		WIDTH 600 HEIGHT 400 ;
		TITLE 'HMG Pause APP Demo' ;
		MAIN ;
		FONT 'Arial' SIZE 10 ;
		ON INIT SimulateDownloads()

		DEFINE MAIN MENU 
			POPUP '&Pause app'
				ITEM 'Pause for 2 minutes' ACTION PauseAPP ( 2 * 60 /* sec */, /* lResume */ )
				ITEM 'Force resume' ACTION PauseAPP ( /* sec */, .T. /* lResume */ )
				ITEM 'Simulate Downloads' ACTION SimulateDownloads()
                        END POPUP
		END MENU

		DEFINE STATUSBAR 
			STATUSITEM ""
		END STATUSBAR

	END WINDOW

	CENTER WINDOW Form_1

	ACTIVATE WINDOW Form_1

Return Nil

************************************************
Procedure SimulateDownloads()
Local i
IF IsControlDefined ( ProgressBar_1 , Form_1 )
	RETURN
ENDIF

Form_1.StatusBar.Item(1) := "The app is working..."
@ 10, 10 PROGRESSBAR ProgressBar_1 OF Form_1 RANGE 0, 65000 WIDTH 550
@ 40, 10 LABEL Label_1 OF Form_1 Value "" WIDTH 550 CENTERALIGN

FOR i := 1 TO 65000
	IF i%10000 = 0		//Every 10000 downloads there is a 20 sec pause.
		PauseAPP ( 20 /* sec */, /* lResume */ )
	ENDIF
	Form_1.ProgressBar_1.Value := i
	Form_1.Label_1.Value := "Downloaded " + Alltrim ( Str ( i ) ) + " of 65000"
	DO Events
NEXT i

Form_1.ProgressBar_1.Release
Form_1.Label_1.Release
Form_1.StatusBar.Item(1) := ""

RETURN

************************************************
Function PauseAPP ( nSec, lResume )
Local nMilliseconds
Local nStartMilliseconds := hb_Milliseconds()
Local cStatusCaption := Form_1.StatusBar.Item(1)

Default nSec := 0, lResume := .F.

nMilliseconds := nSec * 1000

IF lResume
	lPublicForceResume := .T.
	RETURN
ENDIF

Do While hb_Milliseconds() < nStartMilliseconds + nMilliseconds
	IF lPublicForceResume
        	EXIT
	ENDIF
	DO Events
	IF hb_Milliseconds()%1000 = 0
		Form_1.StatusBar.Item(1) := "The app paused, resuming in " + StrZero( ( nStartMilliseconds + nMilliseconds - hb_Milliseconds() ) / 1000, 4, 0 ) + " sec."
	ENDIF
EndDo
lPublicForceResume := .F.
Form_1.StatusBar.Item(1) := cStatusCaption
Return Nil

Re: pause and resume execution

Posted: Mon Oct 07, 2024 11:11 am
by RPC
Hi Edward,
Thank you very much for your quick and correct response. I will incorporate in my program with some modifications.
Pls tell me why have you used Do Events command ? What purpose does it serve ?
Thanks
Rajeev

Re: pause and resume execution

Posted: Mon Oct 07, 2024 11:38 am
by edk
The Do Events command forces the execution of events that are in the queue for execution. This command should be used especially when working in FOR ... NEXT, DO WHILE, etc. loops. Otherwise, the application may not respond to our requests while processing the code executed inside the loop.

For testing purposes, you can remove the Do Events command from the Do While loop in the PauseAPP function and see that while this function is running, you cannot do anything with the application, move the window, change its size, invoke the menu, and after some time the top bar will display information that the application is not responding.

Re: pause and resume execution

Posted: Mon Oct 07, 2024 2:27 pm
by RPC
Thanks , got it. Even though I had seen Do Events command in programs , I never knew its purpose. After removing and adding it in PauseApp program I realised how important it is. Many thanks !
Have a great day!