Call procedure on End

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Env
Enthusiast
Enthusiast
Posts: 151
Joined: Tue Apr 27, 2010 3:20 pm
Location: Wales, United Kingdom

Call procedure on End

Post 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.
Thanks!
User avatar
TomS
Enthusiast
Enthusiast
Posts: 342
Joined: Sun Mar 18, 2007 2:26 pm
Location: Munich, Germany

Re: Call procedure on End

Post 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.
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: Call procedure on End

Post 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.
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Re: Call procedure on End

Post 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).
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Call procedure on End

Post 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()
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
TomS
Enthusiast
Enthusiast
Posts: 342
Joined: Sun Mar 18, 2007 2:26 pm
Location: Munich, Germany

Re: Call procedure on End

Post 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...
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Re: Call procedure on End

Post by eesau »

It's one line shorter :)
Post Reply