I often (really, very often) write console programs. Most of them are simple test programs to test algorithms, calculations or language features, or stuff copied from StackOverflow.
But console programs, if run from the IDE, or from the Windows Explorer, have one annoying feature: they close immediately if they are finished. So if my program has many
Writelns in it, they simply rush by and the console window closes before I can inspect the output. To avoid that, I often put
Readln; in the last line of the program. If the program is finished, it pauses and waits for me to press Enter.
But such a
Readln; is annoying if I really want to run the program from an existing console window (a.k.a. a Windows command line). Then I don’t want to have to press Enter, because the output is shown in the current console window already, and that won’t close until I tell it to.
To get the best of both worlds, I wrote my simple
Velthuis.AutoConsole unit. Put it in the uses clause of your console program and, if the program is a console program and it was started from the command line, or if it is not a console program, it will do nothing. But if it is a console program and it was started any other way (from the Windows Explorer, from the Delphi IDE with or without debugger, from another non-console program), then, before the console window can close, it will display:
Press any key...
Then I really only have to press a key (it does not have to be Enter, it can be any key) and the program will end, closing the console Window.
You can download the code of
Velthuis.AutoConsole by clicking the Download button above.
I hope this code is useful to you. If you use some of it, please credit me. If you modify or improve the unit, please send me the modifications.
I may improve or enhance the unit myself, and I will try to post changes here. But this is not a promise. Please don’t request features.
I do not accept any responsibility about the usability or in case of any damage caused by the use of my programs or files. Use them at your own risk. If they don’t work properly, you have the sources.
Standard Disclaimer for External Links
These links are being provided as a convenience and for informational purposes only; they do not constitute an endorsement or an approval of any of the products, services or opinions of the corporation or organization or individual. I bear no responsibility for the accuracy, legality or content of the external site or for that of subsequent links. Contact the external site for answers to questions regarding its content.
Disclaimer and Copyright
The coding examples presented here are for illustration purposes only. The author takes no responsibility for end-user use. All content herein is copyrighted by Rudy Velthuis, and may not be reproduced in any form without the author's permission. Source code written by Rudy Velthuis presented as download is subject to the license in the files.