[Progress Communities] [Progress OpenEdge ABL] Forum Post: RE: can we rely on when garbage collection fires?

Status
Not open for further replies.
J

jankeir

Guest
Only use with versions that have garbage collection ;-) /* ============================================================================ FILE: utils/safelib.cls ------------------------------------------------------------------------------- PURPOSE: Create a safe way to start libraries that are automatically stopped if the calling procedure ends. Why this works: The registered/started procedure is automatically deleted because the destructor fires automatically when the OBJECT goes out of scope. Example Usage: In the definitions section DEFINE VARIABLE mySafeLib AS utils.safelib NO-UNDO. DEFINE VARIABLE hSomeProcedure AS HANDLE NO-UNDO. Where you want to start a persistent procedure: IF NOT VALID-HANDLE(hSomeProcedure) THEN DO: RUN some/proc.p PERSISTENT SET hSomeProcedure. ASSIGN mySafeLib = NEW utils.safelib(hSomeProcedure). END. Example Usage 2: Advantage of this method: Only one variable needed in calling procedure. Disadvantage: less flexibility: Can't start on appserver, can't start procedures with parameters. In the definitions section DEFINE VARIABLE mySafeLib AS utils.safelib NO-UNDO. Where you want to start a persistent procedure: IF NOT VALID-OBJECT(mySafeLib) THEN DO: ASSIGN mySafeLib = NEW utils.safelib("some/proc.p"). END. To use it: RUN some-internal-proc IN mySafeLib:getHandle(). ============================================================================ */ CLASS utils.safelib: DEFINE VARIABLE hProcedure AS HANDLE NO-UNDO. /* start the procedure from the safelib */ CONSTRUCTOR safelib(INPUT icProcedure AS CHARACTER): RUN VALUE(icProcedure) PERSISTENT SET hProcedure. END CONSTRUCTOR. /* register the procedure here that was started elsewhere, usefull for appserver handles, webservices, * procdures with input Parameters,... */ CONSTRUCTOR safelib(INPUT ihProcedure AS HANDLE): ASSIGN hProcedure = ihProcedure. END CONSTRUCTOR. /* obtain the handle if we didn't start it */ METHOD PUBLIC HANDLE getHandle (): RETURN hProcedure. END METHOD. /* getHandle */ /* clean up */ DESTRUCTOR safelib () : IF VALID-HANDLE(hProcedure) THEN DO: DELETE PROCEDURE hProcedure. END. /* valid-handle */ END DESTRUCTOR. END CLASS.

Continue reading...
 
Status
Not open for further replies.
Back
Top