Interactive PowerBasic Forum

IT-Consultant: Charles Pegge => OxygenBasic => Topic started by: Ed Davis on July 19, 2022, 05:55:12 PM

Title: O2 program termination
Post by: Ed Davis on July 19, 2022, 05:55:12 PM
How do you end an O2 program, if you're not in the main part of the code?

I looked in control_flow.md, but did not see anything.  I'm sure I could have missed it.

Thanks!
Title: Re: O2 program termination
Post by: Chris Chancellor on July 23, 2022, 05:18:02 PM
Hello Ed,

please see Reply #5 for SUB ExitProgram in http://www.jose.it-berater.org/smfforum/index.php?topic=5272.0


use corewin


%PROCESS_TERMINATE= 0x0001


'=========================
'  to terminate the program immediately
SUB ExitProgram
  LOCAL hProc  ,ProcId ,ExitAddress AS sys
  Local  lResult AS LONG
  ProcId = GetCurrentProcessId
  hProc = OpenProcess(BYVAL %PROCESS_TERMINATE, BYVAL 0, BYVAL ProcID)
  IF hProc <> %NULL THEN
    lresult = GetExitCodeProcess(hProc, ExitAddress)
    IF lresult <> 0 THEN
      CALL ExitProcess(ExitAddress)
    ELSE
      CloseHandle hProc
    END IF

  END IF




this terminates the program wherever or whenever it is called, it is a clean exit
Title: Re: O2 program termination
Post by: Zlatko Vid on August 17, 2022, 02:25:10 PM
whole that code for clean exit ?
wow

ok
i am using just  ExitProcess(0)
and looks so far that work
Title: Re: O2 program termination
Post by: Charles Pegge on August 17, 2022, 06:42:45 PM
Programs can be properly terminated with end in the global space. For instance:


gosub greet
end

greet:
print "Helo"
ret
Title: Re: O2 program termination
Post by: Theo Gottwald on August 17, 2022, 10:56:04 PM
Technically a better solution then with PB.
Where you need
SendMessage hWndForm, %WM_CLOSE, 0, 0

I prefer a simple "End" and the Compiler makes all the internal Cleanup.
Does it?
Title: Re: O2 program termination
Post by: Charles Pegge on August 17, 2022, 11:19:35 PM
Yes, o2 does its internal global cleanup, before calling ExitProcess.
Title: Re: O2 program termination
Post by: Nicola on August 18, 2022, 03:40:43 PM
END is used together with other keywords to close loops or blocks.
By itself, in the global space, it is used to terminate the program.
As we can see from the Help page.
Title: Re: O2 program termination
Post by: Zlatko Vid on August 18, 2022, 11:59:47 PM
well i never use END ..
Title: Re: O2 program termination
Post by: Nicola on August 19, 2022, 09:53:01 AM
... actually there are many ways to end a program ...
Sometimes we also use the figure method.