Detecting console close with red X on windows

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Detecting console close with red X on windows

Zaaphod

Is there a way I can detect that a user has pushed the red X in the upper right corner to close the console window in a free pascal console application so I can save some files before the program terminates?

 

James


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Detecting console close with red X on windows

Kevin Lyda
At least on Unix systems I'd assume the answer was to write a signal
handler: https://www.freepascal.org/docs-html/rtl/baseunix/fpsigaction.html

Kevin

On Sun, May 19, 2019 at 1:23 PM James Richters
<[hidden email]> wrote:

>
> Is there a way I can detect that a user has pushed the red X in the upper right corner to close the console window in a free pascal console application so I can save some files before the program terminates?
>
>
>
> James
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal



--
Kevin Lyda
Galway, Ireland
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Detecting console close with red X on windows

wkitty42
On 5/19/19 8:29 AM, Kevin Lyda wrote:
> At least on Unix systems I'd assume the answer was to write a signal
> handler: https://www.freepascal.org/docs-html/rtl/baseunix/fpsigaction.html


IME, that should always be done... especially in the *nix world... you never
know where termination may come from and providing a clean exit is much
preferable to an uncontrolled exit...

--
  NOTE: No off-list assistance is given without prior approval.
        *Please keep mailing list traffic on the list unless*
        *a signed and pre-paid contract is in effect with us.*
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Detecting console close with red X on windows

Henry Vermaak
In reply to this post by Zaaphod
On Sun, 19 May 2019, 13:23 James Richters, <[hidden email]> wrote:

Is there a way I can detect that a user has pushed the red X in the upper right corner to close the console window in a free pascal console application so I can save some files before the program terminates?

You can do that with SetConsoleCtrlHandler() and catching  CTRL_CLOSE_EVENT in the handler.

Henry

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Detecting console close with red X on windows

Zaaphod

Could I please get an example of this?  I see lots of examples of how to do it in C but when I try to translate to pascal I always struggle a quite a bit.

 

James

 

From: fpc-pascal <[hidden email]> On Behalf Of Henry Vermaak
Sent: Sunday, May 19, 2019 8:39 AM
To: FPC-Pascal users discussions <[hidden email]>
Subject: Re: [fpc-pascal] Detecting console close with red X on windows

 

On Sun, 19 May 2019, 13:23 James Richters, <[hidden email]> wrote:

Is there a way I can detect that a user has pushed the red X in the upper right corner to close the console window in a free pascal console application so I can save some files before the program terminates?

You can do that with SetConsoleCtrlHandler() and catching  CTRL_CLOSE_EVENT in the handler.

 

Henry


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Detecting console close with red X on windows

Henry Vermaak
On Sun, 19 May 2019 at 14:33, James Richters
<[hidden email]> wrote:
>
> Could I please get an example of this?  I see lots of examples of how to do it in C but when I try to translate to pascal I always struggle a quite a bit.

Have a look at the fpc source in packages/fv/src/w32msg.inc and search
for SetConsoleCtrlHandler and then HandleConsoleCtrl.  There's nothing
to it, really.

Henry
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Detecting console close with red X on windows

Zaaphod
Thanks!    That was quite helpful!   I have a test program kind of working.  

I notice I only  get 5 seconds to do what I am going to before it closes, and I also notice that my program can no longer respond to keystrokes after I hit the red X.    How do other programs get around this?  For example if I open word for windows and type something and hit the X it asks if I want to save it and will wait indefinitely  for a response.   Is this a limitation because I am running console application,    Is there a way I can do this with my console application as well?

Here is my test program:

uses crt,windows;
var x,y: longint;
    Closetheprogram:Boolean;

Function Console_Handler(dwCtrlType: Dword) : BOOL; stdcall;
Begin
  Console_Handler:=false;
  Case dwCtrlType of
    CTRL_CLOSE_EVENT,
    CTRL_LOGOFF_EVENT,
    CTRL_SHUTDOWN_EVENT :
      begin
         Console_Handler:=True;
         CloseTheProgram:=True;
          Writeln('start');
          For X:= 1 to 2000000 do  //this keeps the Console_Handler busy so my program continues to execute;
              Begin
                 sleep(1);
                 inc(y);
              end;
          Writeln('stop');
        Console_Handler:=True;
        CloseTheProgram:=True;
      end;
  end;
end;

Begin
CloseTheProgram:=False;
if SetConsoleCtrlHandler(@Console_handler, TRUE) then
   Writeln('Console CTRL Handler Installed')
else
   Writeln('Console CTRL Handler Not Installed');
Repeat
Write('.');
Sleep(100);
Until CloseTheProgram or keypressed;

If Keypressed then
Repeat
readkey;
Until not(Keypressed);

Repeat
Write(',');
Sleep(100);
Until keypressed;

Writeln(ClosetheProgram);
end.


-----Original Message-----
From: fpc-pascal <[hidden email]> On Behalf Of Henry Vermaak
Sent: Sunday, May 19, 2019 11:44 AM
To: FPC-Pascal users discussions <[hidden email]>
Subject: Re: [fpc-pascal] Detecting console close with red X on windows

On Sun, 19 May 2019 at 14:33, James Richters <[hidden email]> wrote:
>
> Could I please get an example of this?  I see lots of examples of how to do it in C but when I try to translate to pascal I always struggle a quite a bit.

Have a look at the fpc source in packages/fv/src/w32msg.inc and search for SetConsoleCtrlHandler and then HandleConsoleCtrl.  There's nothing to it, really.

Henry
_______________________________________________
fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Detecting console close with red X on windows

Free Pascal - General mailing list
James Richters <[hidden email]> schrieb am So., 19. Mai 2019, 19:10:
Thanks!    That was quite helpful!   I have a test program kind of working. 

I notice I only  get 5 seconds to do what I am going to before it closes, and I also notice that my program can no longer respond to keystrokes after I hit the red X.    How do other programs get around this?  For example if I open word for windows and type something and hit the X it asks if I want to save it and will wait indefinitely  for a response.   Is this a limitation because I am running console application,    Is there a way I can do this with my console application as well

Correct, that is a limitation of the console no matter the operating system. 

Regards, 
Sven 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Detecting console close with red X on windows

Zaaphod

Thanks for clarifying,  I have everything working well now in my real program,   I didn’t need to do anything other than save and close some files, so I have plenty of time to get everything done.

 

James

 

From: fpc-pascal <[hidden email]> On Behalf Of Sven Barth via fpc-pascal
Sent: Sunday, May 19, 2019 1:15 PM
To: FPC-Pascal users discussions <[hidden email]>
Cc: Sven Barth <[hidden email]>
Subject: Re: [fpc-pascal] Detecting console close with red X on windows

 

James Richters <[hidden email]> schrieb am So., 19. Mai 2019, 19:10:

Thanks!    That was quite helpful!   I have a test program kind of working. 

I notice I only  get 5 seconds to do what I am going to before it closes, and I also notice that my program can no longer respond to keystrokes after I hit the red X.    How do other programs get around this?  For example if I open word for windows and type something and hit the X it asks if I want to save it and will wait indefinitely  for a response.   Is this a limitation because I am running console application,    Is there a way I can do this with my console application as well

 

Correct, that is a limitation of the console no matter the operating system. 

 

Regards, 

Sven 


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal