EXIT (function): Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 23: Line 23:
''Example 1:'' Using an ON TIMER check to read the _EXIT request return values.
''Example 1:'' Using an ON TIMER check to read the _EXIT request return values.
{{CodeStart}}
{{CodeStart}}
q = {{Cl|_EXIT (function)|_EXIT}} 'function read prevents any program exit at start of program
q = {{Cl|_EXIT (function)|_EXIT}} {{Text|<nowiki>'function read prevents any program exit at start of program</nowiki>|#919191}}
{{Cl|ON TIMER (n)|ON TIMER}}(5) {{Cl|GOSUB}} quit
{{Cl|ON TIMER(n)|ON TIMER}}({{Text|5|#F580B1}}) {{Cl|GOSUB}} quit
{{Cl|TIMER}} ON
{{Cl|TIMER}} {{Cl|ON}}
{{Cl|PRINT}} "  The Timer will check for exit request every 5 seconds."
{{Cl|PRINT}} {{Text|<nowiki>"  The Timer will check for exit request every 5 seconds."</nowiki>|#FFB100}}
{{Cl|PRINT}} "Click the X box and/or Ctrl - Break to see the {{Cl|_EXIT (function)|_EXIT}} return!"
{{Cl|PRINT}} {{Text|<nowiki>"Click the X box and/or Ctrl - Break to see the _EXIT return!"</nowiki>|#FFB100}}
{{Cl|PRINT}} "                    Any Key Quits"
{{Cl|PRINT}} {{Text|<nowiki>"                    Any Key Quits"</nowiki>|#FFB100}}
{{Cl|PRINT}}
{{Cl|PRINT}}
{{Cl|DO}}: {{Cl|_LIMIT}} 30
{{Cl|DO}}: {{Cl|_LIMIT}} {{Text|30|#F580B1}}
  '                    ' simulated program loop
    {{Text|<nowiki>'                    ' simulated program loop</nowiki>|#919191}}
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} <> ""
{{Cl|DO...LOOP|LOOP UNTIL}} {{Cl|INKEY$}} <> {{Text|<nowiki>""</nowiki>|#FFB100}}
{{Cl|END}}
{{Cl|END}}


Line 39: Line 39:
{{Cl|IF}} q {{Cl|THEN}} {{Cl|PRINT}} q;
{{Cl|IF}} q {{Cl|THEN}} {{Cl|PRINT}} q;
{{Cl|SELECT CASE}} q
{{Cl|SELECT CASE}} q
  {{Cl|CASE}} 1: {{Cl|PRINT}} "= X button was clicked"
    {{Cl|CASE}} {{Text|1|#F580B1}}: {{Cl|PRINT}} {{Text|<nowiki>"= X button was clicked"</nowiki>|#FFB100}}
  {{Cl|CASE}} 2: {{Cl|PRINT}} "= Ctrl + Break keypress"
    {{Cl|CASE}} {{Text|2|#F580B1}}: {{Cl|PRINT}} {{Text|<nowiki>"= Ctrl + Break keypress"</nowiki>|#FFB100}}
  {{Cl|CASE}} 3: {{Cl|PRINT}} "= Both X and Ctrl + Break!"
    {{Cl|CASE}} {{Text|3|#F580B1}}: {{Cl|PRINT}} {{Text|<nowiki>"= Both X and Ctrl + Break!"</nowiki>|#FFB100}}
{{Cl|END SELECT}}
{{Cl|END SELECT}}
{{Cl|RETURN}}
{{Cl|RETURN}}
{{CodeEnd}}
{{CodeEnd}}


----


''Example 2:'' Removing temporary files before closing a program upon a user's exit request.
''Example 2:'' Removing temporary files before closing a program upon a user's exit request.
{{CodeStart}}
{{CodeStart}}
x = {{Cl|_EXIT}} 'initial function call blocks a user exit
x = {{Cl|_EXIT (function)|_EXIT}} {{Text|<nowiki>'initial function call blocks a user exit</nowiki>|#919191}}
OPEN "t3mpdata.tmp" FOR APPEND AS #1
{{Cl|OPEN}} {{Text|<nowiki>"t3mpdata.tmp"</nowiki>|#FFB100}} {{Cl|OPEN#File_Access_Modes|FOR}} {{Cl|OPEN#File_Access_Modes|APPEND}} {{Cl|OPEN|AS}} #1
DO
{{Cl|DO}}
IF {{Cl|_EXIT}} THEN {{Cl|CLOSE}}: {{Cl|KILL}} "t3mpdata.tmp": {{Cl|_DELAY}} 1: {{Cl|SYSTEM}}
    {{Cl|IF}} {{Cl|_EXIT (function)|_EXIT}} {{Cl|THEN}} {{Cl|CLOSE}}: {{Cl|KILL}} {{Text|<nowiki>"t3mpdata.tmp"</nowiki>|#FFB100}}: {{Cl|_DELAY}} {{Text|1|#F580B1}}: {{Cl|SYSTEM}}
LOOP
{{Cl|LOOP}}
{{CodeEnd}}
{{CodeEnd}}
<center>{{Text|Note: If you have a file named ''t3mpdata.tmp'' change the file name!|red}}</center>
<center>{{Text|Note: If you have a file named ''t3mpdata.tmp'' change the file name!|red}}</center>
Line 59: Line 60:


{{PageSeeAlso}}
{{PageSeeAlso}}
* [https://qb64phoenix.com/forum/showthread.php?tid=1257 Featured in our "Keyword of the Day" series]
* [[SYSTEM]]
* [[SYSTEM]]
* [[END]]
* [[END]]

Latest revision as of 18:07, 25 May 2024

The _EXIT function prevents the user from closing a program and indicates if a user has clicked the close button in the window title (X button) or used CTRL + BREAK.


Syntax

exitSignal% = _EXIT


Description

  • Once the _EXIT function is used, the user can no longer manually exit the program until it is ended with END or SYSTEM.
  • _EXIT returns any exit requests made after the initial call as:
0 = no exit request has been made since _EXIT monitoring began in the program.
1 = exit attempted by clicking the window X (close) button since last function call. (Bit 0 set)
2 = exit attempted with CTRL + BREAK since last call. (Bit 1 set)
3 = both CTRL + BREAK and the X box have been used since last call. (Bit 0 and 1 set)
  • If a return value is not 0 the program can handle an exit request at a more convenient time if necessary.
  • After being read, the _EXIT value is reset to 0 so store the value when a program delays an exit request.
  • Note: Once _EXIT has been used once, you must monitor your program by checking it for user _EXIT requests.
  • Don't just use _EXIT once to prevent a user from exiting a program early, as that constitutes bad practice.


Examples

Example 1: Using an ON TIMER check to read the _EXIT request return values.

q = _EXIT 'function read prevents any program exit at start of program
ON TIMER(5) GOSUB quit
TIMER ON
PRINT "  The Timer will check for exit request every 5 seconds."
PRINT "Click the X box and/or Ctrl - Break to see the _EXIT return!"
PRINT "                    Any Key Quits"
PRINT
DO: _LIMIT 30
    '                    ' simulated program loop
LOOP UNTIL INKEY$ <> ""
END

quit:
q = _EXIT
IF q THEN PRINT q;
SELECT CASE q
    CASE 1: PRINT "= X button was clicked"
    CASE 2: PRINT "= Ctrl + Break keypress"
    CASE 3: PRINT "= Both X and Ctrl + Break!"
END SELECT
RETURN

Example 2: Removing temporary files before closing a program upon a user's exit request.

x = _EXIT 'initial function call blocks a user exit
OPEN "t3mpdata.tmp" FOR APPEND AS #1
DO
    IF _EXIT THEN CLOSE: KILL "t3mpdata.tmp": _DELAY 1: SYSTEM
LOOP
Note: If you have a file named t3mpdata.tmp change the file name!


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link