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

Pause program execution

Status
Not open for further replies.

hjohnson

Programmer
Aug 1, 2001
90
0
0
US
Hi all,

I've got a background process that takes data from VFP tables and stores them in SQL- tables. It's been working great except for the fact that the sql-servers are taken offline during the night which causes connectivty errors. I've trapped for the error (1526) but I want to issue some type of a pause for about 20 minutes or so to allow time for them to come back online. I thought about using a wait command with a timeout of about 1200 seconds but not sure if it is the best way or if someone has a better idea.

Thanks

Hal
 
Hi Hal,

How about something like this (untested):

-----------
Store 0 to nMin, nSec
On error lError = .T.
lError = .T.
lConnect = .F.
Do while (nMin < 20) and lError
lError = .F.
** Get the connection
x = CreateObject('SQLServer')
If lError
Wait window nowait 'Connection error!' + chr(13) + ;
'Start SQL-Server or press ESCAPE to continue'
= inkey(1, 'HM') && wait a keystroke for 1 second
if (lastkey() != 27)
nSec = nSec + 1
nMin = nSec / 60 && Elapsed time in minutes
else
lError = .F.
endif
else
lConnect = .T.
endif
enddo
On error

If lConnect
** Connected
** Do the activity
endif
-------------

Hope it helps

-- AirCon --
 
Hi,

Nothing wrong with a long timeout but this may be more helpfull.

=paws(20)

FUNCTION paws
PARAMETERS mins
DO WHILE mins<>0
WAIT WINDOW &quot;Waiting for &quot;+STR(mins,3,0)+&quot; minutes....&quot; TIMEOUT 60
IF LASTKEY()=27
mins = 1
endif
mins = mins - 1
ENDDO
RETURN
 
Thanks for all the response. They will indeed help. I must admit that the 'Sleep' option is intriguing, I'm not very familiar with all the kernel32 function available and would love to know more- especially ones that can be useful in VFP. One question I do have- does the declare sleep cause a 'sleep' for the entire workstation or just for the VFP app that called it. I would hate to invoke something that cause problems elsewhere.

Thanks again to all.

Hal
 
Hal,

A really good place to find out about this API functions is at
does the declare sleep cause a 'sleep' for the entire workstation or just for the VFP app that called it.

Don't worry. It only affects the process which calls it. The visible effect is to disable the keyboard and mouse (you don't even see the hourglass) while the application has focus. You can still use the mouse or Alt-Tab to switch to other apps, and those apps will function normally.

One other point. With all these API functions, you only need to execute the DECLARE once during the session. Once yuo have done so, the declaration stays in memory, and you can then call the function from anywhere in the application.

Mike


Mike Lewis
Edinburgh, Scotland
 
Hal,
Sleep function pauses only the current thread for a given time. But, if you tried it, you probably noticed that no other event processing occurs during sleep time (e.g. you cannot move a form belonging to the application), it's more like a clinical death. You can get the same thing with INKEY(nTimeout_in_seconds).

So, my opinion is that better solutions are:

1. good, but not best
PROCEDURE Wait4me
LPARAMETERS HowLong &&seconds
LOCAL i, nKey
nKey = 0
i=0
Do while i<Howlong/0.1
nKey = inkey(0.1, 'H')
i=i+1
DoEvents
enddo
ENDPROC

2. best
PROCEDURE Wait4Me
LPARAMETERS HowLong &&seconds
LOCAL oTimer
oTimer=CREATEOBJECT('MyTimer', .T., HowLong)
DO WHILE TYPE('oTimer.name')='C'
DOEVENTS
ENDDO
ENDPROC

DEFINE CLASS MyTimer as Timer
PROCEDURE Init
PARAMETERS ena, interv
this.Enabled=ena
this.Interval=1000*interv
ENDPROC
PROCEDURE timer
this.enabled=.F.
RELEASE this
ENDPROC
ENDDEFINE

Credit to AirCon for the first solution.
HTH
Good luck!

 
What about using a Timer Control?

Attitude is Everything
 
Danceman,

What about using a Timer Control?

A timer control doesn't really pause execution. On the contrary, it generates an event at a pre-determined interval. Maybe there is some way of using to achieve Hal's goal, but it's not the first thing that comes to mind.

Mike


Mike Lewis
Edinburgh, Scotland
 
I really appreciate the responses that have been given. I tried the Sleep funtion without any success. It stopped the process allright, but it never restarted. Perhaps it was where it was located that was the the problem. The purpose of the pause is to stop the process to allow the SQL-Server to come back online. I trap for an Connectivty error (#1526) and placed the sleep function there with a value of 5 minutes (1000*60*5). I started my process, then stopped the SQL-Server. My process stopped as it should have. I then re-started SQL-Server well within the 5 minutes. After about 15 minutes, the VFP process never came out of the sleep- I left it overnight with hopes it would wake and continue- This morning I had to end-task to close it. Could having the code in the error method of form be a culprit.

Thanks

Hal
 
Below is the code I was using that doesn't seem to be working:

** Error Method of Form **
LPARAMETERS nError, cMethod, nLine

Do Case
CASE nError=1526
*....
*....
TABLEREVERT()
**Connection problem. Add a pause
lcScreenCap=_vfp.caption
_vfp.caption='Wait for SQL ReStart' &&Change Taskbar descript to be meaningful to use

DECLARE Sleep IN kernel32 INTEGER
lnsleeptime= 1000*60*5 && Set in miliseconds
Sleep(lnSleepTime)

_vfp.caption=lcScreenCap &&Reset Description

RETRY
Othwise
***
***
Endcase
 
hjohnson,

<<
The purpose of the pause is to stop the process to allow the SQL-Server to come back online
>>

The code that I posted does exactly what you want. All you need is to adjust the INKEY() into how long do you want it to wait. Also using inkey(), you give the user the alternative to press the key &quot;IF&quot; the SQL back online or just press ESCAPE if they want to abort. So they don't have to wait any longer.

But that's just my 0.02$ :)
Regards

-- AirCon --
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top