FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Function that runs no matter how the program is terminated
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Function that runs no matter how the program is terminated
Posted: Wed Jan 28, 2015 08:16 PM

I have a program that I am running as a service.
Is there any way to run a function if the service is stopped for any reason?

I keep a log and add to it for various items but would like to add an entry when the service stops.

Thanks,

Jeff Barnes



(FWH 16.11, xHarbour 1.2.3, Bcc730)
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Function that runs no matter how the program is terminated
Posted: Wed Jan 28, 2015 09:29 PM
Jeff,

There is a function that runs automatically on exit.

Code (fw): Select all Collapse
Exit Function App_Exit()

    // do whatever

Return NIL


I don't expect it will run on a crash though.

James
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Re: Function that runs no matter how the program is terminated
Posted: Thu Jan 29, 2015 12:07 AM
Hi James,

Is there anything that needs to be linked for this to work?
I have added the code but it seems to do nothing.

Code (fw): Select all Collapse
EXIT Function  App_Exit()
    oLog:Add("*************************EXIT FUNCTION: EzSatSE Import Server Stopped*****************************"+DTOC(Date())+"  "+TIME())
    CLOSE CONFIG
    oLog:End()
RETURN NIL


The text doesn't show up in the log file.
Thanks,

Jeff Barnes



(FWH 16.11, xHarbour 1.2.3, Bcc730)
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Function that runs no matter how the program is terminated
Posted: Thu Jan 29, 2015 01:17 AM

You need not add any other library.
This works whenever the program exists "normally" but not when the process is "killed".

1) In our normal Exe's this EXIT function/procedure is not executed when a run-time error is encountered because PostQuitMessage( 0 ) is used in the errosysw.prg.

In other words, EXIT procedure/function works after the main() function ends or after execution of a normal QUIT statement.
If PostQuitMessage(0) is executed, nothing in our program is executed and not even the EXIT procedure.

Another thing you may check is whether oLog:Add(...) function works after the log file is closed and if you are closing log file before the Main() function ends.

Regards



G. N. Rao.

Hyderabad, India

Continue the discussion