On Exit question
#29
I leave for one day and y'all go down a rabbit hole.... Great read. I will have to play more with the EXIT in something simplier before I try to start inserting it into my main project. $NOPREFIX and OPTION EXPLICIT are my first two lines whenever I start a new program for lots of reason discussed. Not only it helps prevent silly errors but keeps from being lazy with my DIM statements.

(06-02-2023, 03:14 PM)bplus Wrote: @NasaCow Search can't find ON EXIT anywhere in your code you posted.

This is what I do find:

Code: (Select All)
'Disabling the default exit routinue
ExitFlag = EXIT
ON TIMER(1) GOSUB ShutDown
TIMER ON


Code: (Select All)
ShutDown:
ExitFlag = EXIT
IF ExitFlag THEN SYSTEM
RETURN

Does not look good to set ExitFlag Twice nor putting Shutdown on Timer.

When you know the user wants to quit from _Exit then you run the save work code if not saved and quit or just quit if work already saved. 

I don't see need for Timer sub here.

This is the example in the Wiki more or less, and where I decided to start to try to learn something new. This the example code for _EXIT (function) - https://qb64phoenix.com/qb64wiki/index.p...(function)

You can see On Exit it isn't used here either but checks every 5 seconds with ON TIMER. The first ExitFlag is used to disable the X and setup the check with the timer. Then we evaluate it every so oftern. I will have to play with ON EXIT and see if I can get that to work (on first try, it was a no go so...)
Code: (Select All)
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

I am all for doing it a different way, just don't know how to go about it since I never tried to control how a program closes before.



Thank you for remembering about me and I don't mind a discussion goes off track as well as helps. I ALWAYS use Option _Explicit in all my programs for that reason alone. I find it lazy programing to not DIM what you need. It makes it so much easier for other people to read the code and know what's going on. Personally, I DIM my throw away variables and give them a name like Counter or CounterX or CounterY, I just find it easy for coding when everything is named in such a way that when I come back a few months later, I still can understand it.
(06-03-2023, 01:29 PM)Dimster Wrote: 1st I apologize to NasaCow, hope the discussion on Option _Explicit may help with the search for/correction of the error you were looking for help on.

2nd ... the issue/learning lesson, for me in this thread is Dimensioned variables v's UnDimensioned variables. In particular a control variable. I have often seen the use of "i" or "j" or "x" being used in many books on Qbasic where these control variables are not Dimmed. They appear to be throw away variables in terms of their use is not material to math formulas or capture of a results of a process. They can be used over and over again as a control variable because their type is defined by default and their value is clearly defined in a range (ie 1 to 10). The only time I will Dim a control variable like "x" is if I need it to carry over into a subroutine or back to the main module.

The Dimming of a control variable, in my experience just added to multiple lines of Dim and Dim Shared in a large program. Plus it doesn't help with the ending value , or limiting the control range. For example 

Code: (Select All)
Dim Shared y(1 To 10)
Dim x

For x = 1 To 10
    Print "Hello World"
Next
Print
Print x
Print
Print
For y = 1 To 10
    Print "Hello back at ya"
Next
Print
Print y

In each case x and y finished outside of the control range, so I have to be careful when I do Dim x or y to use in another sub or function. 

Not sure I see the value in Option _Explicit for control variables, nor how Dimensioning all control variables is a "Best Practice" but this old dog is definitely learning new tricks by trial and error especially when some masterfully programmers are highlighting the new trick.

Thanks for all the suggestions and this became an interesting thread in general!
WHILE NOT EndOfLife(1)
    HappyLife = HappyWife - (Money * Time * Travel * Gifts)
    Kids = (NoTime * LackOfLove) MOD NumOfKids
    IF Retirement <> Rich THEN YearsOnJob = YearsOnJob + 1 ELSE SeeTheWorld
WEND
Reply


Messages In This Thread
On Exit question - by NasaCow - 06-02-2023, 03:50 AM
RE: On Exit question - by mnrvovrfc - 06-02-2023, 05:29 AM
RE: On Exit question - by NasaCow - 06-02-2023, 06:00 AM
RE: On Exit question - by mnrvovrfc - 06-02-2023, 08:59 AM
RE: On Exit question - by Dimster - 06-02-2023, 11:19 AM
RE: On Exit question - by TerryRitchie - 06-02-2023, 11:27 AM
RE: On Exit question - by Dimster - 06-02-2023, 12:28 PM
RE: On Exit question - by Kernelpanic - 06-02-2023, 01:54 PM
RE: On Exit question - by TerryRitchie - 06-02-2023, 05:33 PM
RE: On Exit question - by bplus - 06-02-2023, 01:28 PM
RE: On Exit question - by bplus - 06-02-2023, 02:21 PM
RE: On Exit question - by Kernelpanic - 06-02-2023, 08:23 PM
RE: On Exit question - by bplus - 06-02-2023, 02:36 PM
RE: On Exit question - by bplus - 06-02-2023, 03:14 PM
RE: On Exit question - by bplus - 06-02-2023, 08:19 PM
RE: On Exit question - by bplus - 06-02-2023, 08:27 PM
RE: On Exit question - by Kernelpanic - 06-02-2023, 08:40 PM
RE: On Exit question - by Dimster - 06-02-2023, 09:33 PM
RE: On Exit question - by mnrvovrfc - 06-02-2023, 09:44 PM
RE: On Exit question - by TerryRitchie - 06-02-2023, 10:52 PM
RE: On Exit question - by bplus - 06-02-2023, 10:59 PM
RE: On Exit question - by TerryRitchie - 06-02-2023, 11:12 PM
RE: On Exit question - by bplus - 06-02-2023, 11:21 PM
RE: On Exit question - by Dimster - 06-03-2023, 01:29 PM
RE: On Exit question - by SMcNeill - 06-03-2023, 01:51 PM
RE: On Exit question - by Dimster - 06-03-2023, 03:10 PM
RE: On Exit question - by TerryRitchie - 06-03-2023, 07:21 PM
RE: On Exit question - by Kernelpanic - 06-03-2023, 03:26 PM
RE: On Exit question - by NasaCow - 06-04-2023, 03:03 AM
RE: On Exit question - by bplus - 06-04-2023, 03:20 PM
RE: On Exit question - by TempodiBasic - 06-04-2023, 04:05 PM
RE: On Exit question - by TempodiBasic - 06-04-2023, 05:38 PM
RE: On Exit question - by TerryRitchie - 06-04-2023, 08:35 PM
RE: On Exit question - by TempodiBasic - 06-04-2023, 10:51 PM
RE: On Exit question - by NasaCow - 06-04-2023, 11:59 PM
RE: On Exit question - by TerryRitchie - 06-05-2023, 12:31 AM
RE: On Exit question - by NasaCow - 06-05-2023, 12:50 AM
RE: On Exit question - by SMcNeill - 06-05-2023, 09:04 PM
RE: On Exit question - by bplus - 06-05-2023, 09:09 PM
RE: On Exit question - by SMcNeill - 06-05-2023, 09:17 PM
RE: On Exit question - by SMcNeill - 06-07-2023, 01:38 AM
RE: On Exit question - by TempodiBasic - 06-07-2023, 09:08 AM
RE: On Exit question - by TempodiBasic - 06-07-2023, 09:31 AM
RE: On Exit question - by NasaCow - 06-08-2023, 12:05 AM
RE: On Exit question - by mnrvovrfc - 06-09-2023, 09:31 PM
RE: On Exit question - by TempodiBasic - 06-09-2023, 09:14 PM
RE: On Exit question - by bplus - 06-10-2023, 05:24 PM
RE: On Exit question - by TempodiBasic - 06-17-2023, 12:18 AM
RE: On Exit question - by bplus - 06-17-2023, 02:29 AM
RE: On Exit question - by GareBear - 06-17-2023, 01:07 AM
RE: On Exit question - by TempodiBasic - 06-22-2023, 12:01 PM
RE: On Exit question - by bplus - 06-22-2023, 12:22 PM
RE: On Exit question - by TempodiBasic - 06-22-2023, 07:45 PM



Users browsing this thread: 23 Guest(s)