Program Not Closing ON CLOSE

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
Post Reply
MattC
Posts: 49
Joined: Mon 16 Apr 2018, 06:17

Program Not Closing ON CLOSE

Post by MattC » Sat 01 Jun 2019, 06:30

Hi. This one's got me stumped.

The following bit of code is an adaptation from a part of my program.

Ignore the fact that DP% is always set to -1, as this is a normal posibility of the removed code. When the window is clicked on the loop runs to the error message. When the window 'X' close button is clicked on the same thing happens. Why does the system not pass to the PROC_CLOSE routine and QUIT? The close action is happening during the second loop of PROCAction. Surely, PROC_CLOSE should be actioned before it exits this loop, and I can't see anything else that should cause the system to ignore it.

Code: Select all

      ON CLOSE PROC_CLOSE : RETURN

      DP% = -1
      DIM Mouse{X%, Y%, B%}

      REPEAT
        IF DP% < 0 THEN
          PROC_DP
        ENDIF
      UNTIL DP% > -1
      END

      DEF PROC_DP
      LOCAL K%, C%
      REPEAT
        IF DP% = -1 THEN SYS "MessageBox", @hwnd%, "Cannot continue until DP is not -1.", "Error", 64
        W% = FNAction(K%, C%)
       DP% = -1
     UNTIL K% = 27 OR DP% <> -1
      ENDPROC

      DEF FNAction(RETURN K%, RETURN C%)
      LOCAL W%
      C% = 0
      REPEAT
        K% = INKEY(1)
        MOUSE Mouse.X%, Mouse.Y%, Mouse.B%
      UNTIL K% = -1 AND Mouse.B% = 0
      REPEAT
        K% = INKEY(1)
        MOUSE Mouse.X%, Mouse.Y%, Mouse.B%
        SYS "GetForegroundWindow" TO W%
      UNTIL K% <> -1 OR Mouse.B% <> 0
      IF W% = @hwnd% THEN = TRUE ELSE = FALSE

      DEF PROC_CLOSE
      QUIT
      ENDPROC
Matt

p_m21987
Posts: 125
Joined: Mon 02 Apr 2018, 21:51

Re: Program Not Closing ON CLOSE

Post by p_m21987 » Sat 01 Jun 2019, 12:53

I tried testing this program and it does close when I click on the X close button.

MattC
Posts: 49
Joined: Mon 16 Apr 2018, 06:17

Re: Program Not Closing ON CLOSE

Post by MattC » Sat 01 Jun 2019, 19:24

OK. That's interesting. :?

I've only got the one computer to try it on, so I'd be interested if anyone else has tried it, and what results they have. I retried it after a reboot and came up with the same result as before.

Matt

Zaphod
Posts: 73
Joined: Sat 23 Jun 2018, 15:51

Re: Program Not Closing ON CLOSE

Post by Zaphod » Mon 03 Jun 2019, 17:08

So on my machine it did not close either, but then for some reason after many attempts trying to move the dialog and clicking background windows etc. it suddenly started closing and kept doing that from then on. No ideas.
But your program clearly needs ON MOUSE as an interrupt will sort that I am sure. The note in the manual implies that is the way to go.

Z

MattC
Posts: 49
Joined: Mon 16 Apr 2018, 06:17

Re: Program Not Closing ON CLOSE

Post by MattC » Tue 04 Jun 2019, 05:34

Thanks Zaphod.

My understanding of the help for MOUSE and ON MOUSE is that ON MOUSE is an alternative rather than a necessary part of MOUSE. It's possible that using the ON MOUSE command might sort my problem out, but it would mean a heck of a lot of changes to my code. I'd be interesting to know, though, what you did to get it working continually, and whether putting it back reverted to the original problem.

Matt

Zaphod
Posts: 73
Joined: Sat 23 Jun 2018, 15:51

Re: Program Not Closing ON CLOSE

Post by Zaphod » Tue 04 Jun 2019, 13:33

I am afraid I don't know what caused the change in behaviour and I have never managed to get it to do it again spontaneously.
However, putting a WAIT 10 after SYS "GetForegroundWindow" TO W% seemed to fix it.
So it looks like a timing issue and how fast the screen window changes focus which may well be hardware related.

Z

MattC
Posts: 49
Joined: Mon 16 Apr 2018, 06:17

Re: Program Not Closing ON CLOSE

Post by MattC » Tue 04 Jun 2019, 19:51

Cheers. I'll investigate this. One of my thoughts was to add wait statements to various points and it seemed to have little effect. However, I'll try again.

Matt

svein
Posts: 26
Joined: Tue 03 Apr 2018, 19:34

Re: Program Not Closing ON CLOSE

Post by svein » Wed 05 Jun 2019, 08:48

ON CLOSE AND ON MOUSE is triggered by mouse button release.
Your code does not wait for the mouse button to be released before issuing a new SYS "MessageBox", wich is then blocking the ON CLOSE interrupt.
The solution is to wait for button release or insert a long enough wait in the SYS line.

Code: Select all

      REPEAT
        K% = INKEY(1)
        MOUSE Mouse.X%, Mouse.Y%, Mouse.B%
        SYS "GetForegroundWindow" TO W%
      UNTIL K% <> -1 OR Mouse.B% <> 0
      WHILE Mouse.B% : WAIT 0 : MOUSE Mouse.X%, Mouse.Y%, Mouse.B% : ENDWHILE
      IF W% = @hwnd% THEN = TRUE ELSE = FALSE
or

Code: Select all

IF DP% = -1 THEN WAIT 50 : SYS "MessageBox", @hwnd%, "Cannot continue until DP is not -1.", "Error", 64
Svein

MattC
Posts: 49
Joined: Mon 16 Apr 2018, 06:17

Re: Program Not Closing ON CLOSE

Post by MattC » Wed 05 Jun 2019, 17:58

This is a very useful piece of information, and the added code works great.

Svein, thanks.

Matt

Post Reply