Announcement

Collapse
No announcement yet.

SetConsoleCtrlHandler - Close using "X"

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • SetConsoleCtrlHandler - Close using "X"

    'Is is okay to use SetConsoleCtrlHandler?
    'Haven't seen anything on closing files before exiting
    '
    'Can't see this line ever being executing
    'SetConsoleCtrlHandler CODEPTR(ConsoleHandler), 0
    'Is this an improper shutdown?
    Code:
    'Code from Peter Lameijn
    #COMPILE EXE
    #INCLUDE "Win32api.inc"
    
    FUNCTION ConsoleHandler(BYVAL dwEvent AS DWORD) AS LONG
      IF dwEvent = %CTRL_CLOSE_EVENT THEN
        PRINT "You tried closing the console, do you want to lose your work?"
        '...
        PRINT "Here you can save your stuff...
        SLEEP  3000
        FUNCTION = 0  'end immeidately if 0
      END IF
    END FUNCTION
    
    FUNCTION PBMAIN()AS LONG
      LOCAL x AS LONG
      PRINT "Try closing the console by pressing 'X' key"
      PRINT "Press <Esc> to end..."
      SetConsoleCtrlHandler CODEPTR(ConsoleHandler), 1
    
      
      WHILE 1
         INCR x
         LOCATE 10,1:PRINT "you are here";x
        
      SLEEP 10
      IF INSTAT THEN
        IF INKEY$ = $ESC THEN
          PRINT "Exiting program..."
          SLEEP 1000
          EXIT LOOP
        END IF
      END IF
      WEND
      '
      '
      '
      '
      PRINT "This code will never execute
      SetConsoleCtrlHandler CODEPTR(ConsoleHandler), 0
    END FUNCTION
    http://www.powerbasic.com/support/fo...ting+XP+from+c losing+console+window

    ------------------


    [This message has been edited by Mike Doty (edited January 30, 2006).]
    How long is an idea?

  • #2
    >Can't see this line ever being executing
    >'SetConsoleCtrlHandler CODEPTR(ConsoleHandler), 0

    ???
    It doesn't produce any output; there's nothing to see.
    ???


    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      The line before it is a print statement.

      ------------------
      How long is an idea?

      Comment


      • #4
        So the line you never see execute is the PRINT?

        Silly me, I read the question too carefully.

        That said, I'll bet SetConsoleCtrlHandler is doing something strange and wondrous to INSTAT resulting in an endless loop which results in the program exiting directly on the default handler on CTRL_CLOSE_EVENT. Try setting FUNCTION=TRUE on that event and see what happens.


        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          Program ending box from Windows.

          ------------------
          How long is an idea?

          Comment


          • #6
            Code is only executed on a normal program ending (pressing <ESC> in this case)
            Using the close [X] (or Ctrl-Break) is not a normal ending. It will immediately end the program


            ------------------
            Regards,
            Peter
            Regards,
            Peter

            Comment


            • #7
              Code:
              Peter,
              Closes immediately after whatever code you want
              to execute in the ConsoleHandler function.
              In this case it would be fine with me as long
              as it doesn't cause any problems.
              
              FUNCTION ConsoleHandler(BYVAL dwEvent AS DWORD) AS LONG
                IF dwEvent = %CTRL_CLOSE_EVENT THEN
                  PRINT "You tried closing the console, do you want to lose your work?"
                  '...
                  PRINT "Here you can save your stuff...
                  SLEEP  3000
                  FUNCTION = 0  'end immeidately if 0
                END IF
              END FUNCTION
              ------------------
              How long is an idea?

              Comment


              • #8
                Closes immediately after whatever code you want to execute in the ConsoleHandler function. In this case it would be fine with me as long as it doesn't cause any problems.
                Mike,
                The close can occur before the end of 'whatever code'.
                Try this test..
                Code:
                #BREAK ON           ' < needed for PBCC60^
                #COMPILE EXE
                #INCLUDE "Win32api.inc"
                 
                FUNCTION ConsoleHandler(BYVAL dwEvent AS DWORD) AS LONG
                  IF dwEvent = %CTRL_CLOSE_EVENT THEN
                    PRINT "You tried closing the console, do you want to lose your work?"
                    '...
                    PRINT "Here you can save your stuff..."
                    Call YourStuff() : WinBeep 800,200
                    SLEEP  3000
                    FUNCTION = 0  'end immeidately if 0
                  END IF
                END FUNCTION
                 
                SUB YourStuff()
                 LOCAL x AS LONG
                   FOR x = 1 to 10   ' try 1 to 3
                     sleep 1000
                     txt.print x
                   Next
                END SUB
                 
                FUNCTION PBMAIN()AS LONG
                  LOCAL hTxt AS DWORD : TXT.WINDOW ("CLOSE_EVENT", 5,500,20,10) TO hTxt
                  LOCAL x AS LONG
                  PRINT "Try closing the console by pressing 'X' key"
                  PRINT "Press <Esc> to end..."
                  SetConsoleCtrlHandler CODEPTR(ConsoleHandler), 1
                 
                  WHILE 1
                     INCR x
                     LOCATE 10,1:PRINT "you are here";x
                 
                  SLEEP 10
                  IF INSTAT THEN
                    IF INKEY$ = $ESC THEN
                      PRINT "Exiting program..."
                      SLEEP 1000
                      EXIT LOOP
                    END IF
                  END IF
                  WEND
                  '
                  '
                  PRINT "This code will never execute"
                  SetConsoleCtrlHandler CODEPTR(ConsoleHandler), 0
                END FUNCTION
                Possible solution.
                Last edited by Dave Biggs; 24 Jan 2021, 06:37 PM. Reason: Possible solution here: https://forum.powerbasic.com/forum/user-to-user-discussions/powerbasic-console-compiler/804162-break-on-off-other?p=804332#post804332
                Rgds, Dave

                Comment


                • #9
                  I ran. Not sure what to look for. I don't use console programs, but it might be useful for someone.
                  Yes, it will end before "whatever code" whatever that is. This thread is closed "to me" (16-years). Thank you, Dave.
                  How long is an idea?

                  Comment


                  • #10
                    Originally posted by Dave Biggs View Post
                    Code:
                    FUNCTION ConsoleHandler(BYVAL dwEvent AS DWORD) AS LONG
                    IF dwEvent = %CTRL_CLOSE_EVENT THEN
                    PRINT "You tried closing the console, do you want to lose your work?"
                    '...
                    PRINT "Here you can save your stuff..."
                    Call YourStuff() : WinBeep 800,200
                    SLEEP 3000
                    FUNCTION = 0 'end immeidately if 0
                    END IF
                    END FUNCTION
                    Isn't that redundant? When would it ever NOT be 0 with this code?

                    Comment


                    • #11
                      Isn't that redundant?
                      Aye Captain.
                      Rgds, Dave

                      Comment

                      Working...
                      X