Page 1 of 1

Call procedure on End

Posted: Sun Apr 17, 2011 11:07 pm
by Env
Hey,

Would be nice to see a feature implemented where the programmer can 'register' a procedure that automatically gets called when the application ends.
I know it could just be added at the bottom of the source code, but in instances where 'End' is called, or the console is closed etc, it wouldn't apply.

For example, I use a piece of hardware that acts as a kind of relay board, controlled by a DLL, connected by USB.. If I don't explicitly uninitialize the library, it causes a crash, and with it being a console program, a program closure can happen at any time, so having a function that will be called without fail ensures I can correctly release the library, thus preventing unwanted effects.

Obviously the functionality of this feature can be applied to all kinds of situations.

Re: Call procedure on End

Posted: Sun Apr 17, 2011 11:17 pm
by TomS
I don't know if there's a way to intercept closing a console, but here's how I would do it.

Code: Select all

Macro Quit
	Debug "END called"
	End 
EndMacro 

hWnd = OpenWindow(#PB_Any, 0, 0, 800, 600, "", #PB_Window_ScreenCentered|#PB_Window_MinimizeGadget)

Repeat
	event = WaitWindowEvent(20)
	
Until event = #PB_Event_CloseWindow

Quit
Just register Quit as a Keyword in the preferences and you're good to go.

Re: Call procedure on End

Posted: Tue Apr 19, 2011 4:33 am
by kenmo
I use this in almost every program I write:

Code: Select all

Procedure Shutdown()
  ; Free up memory, lists, libraries, etc.

  ; Save settings, yada yada, other finalization

  End
EndProcedure
And then I only call Shutdown() from elsewhere in the program, never call End.

Re: Call procedure on End

Posted: Tue Apr 19, 2011 6:26 am
by eesau
You can do it like this too:

Code: Select all

Procedure Shutdown()
EndProcedure

;...

End Shutdown()
This way you can have several Shutdown() procedures for doing different things (mostly dependant on what you have to clean up).

Re: Call procedure on End

Posted: Tue Apr 19, 2011 3:29 pm
by skywalk
Thanks eesau :shock:
I thought End [ExitCode] only meant a simple numeric like End 999.

Code: Select all

Procedure Shutdown()
  i = -1  
  Debug i
EndProcedure
Global.i i
i = 10
Debug i
End Shutdown()

Re: Call procedure on End

Posted: Tue Apr 19, 2011 3:33 pm
by TomS
So what's the difference between

Code: Select all

ShutDown()
End
and

Code: Select all

End ShutDown()
:?:

Except for the obivious reasons that the return of ShutDown() can somehow be obtained by another program...

Re: Call procedure on End

Posted: Tue Apr 19, 2011 4:42 pm
by eesau
It's one line shorter :)