This is sort of a weird idea and not a direct solution to your question and there are some issues to be dealt with however, in theory this kludge would work...

Since the problem occurs when closing the console window by the "x", make it impossible for the user to do that!
I am going partly from memory on this one, but if you start a Tk gui as a detached process (no console window), it is easy to intercept hitting the "x" on the gui window. There are a couple of ways: see Re: Catching WINDOW close event and take actions and from my memory any code right after Mainloop; executes after the "x" is clicked.

I had one GUI app where I implemented some code after Mainloop; to make sure all of the files were saved if the user clicked "x" instead of using the gui to "save and exit" - it is possible to restart the Mainloop. You don't need any fancy buttons...a simple text display widget would be enough. It is possible to have a routine run right after the Mainloop starts (no user "click" required), but I don't remember the exact way to do that.

On a different tack, why are the user's hitting the console "x" button in the first place?
In my experience that is because they think that the program is hung and because of that, closing the window won't matter.

See if you can just print a "dot", "." every so many work units. Do not have a separate timing routine that prints a dot every 3 seconds or whatever. The users will figure out that that is BS. Make each dot represent some unit of actual work performed. This typically will lead to a jerky timing of dots and the users trust that more than some kind of regularly timed dots. Maximum user patience is somewhat south of 90 seconds.

It is also possible that your program shouldn't even have a constantly displayed UI! In Unix lingo this is a daemon, in Windows lingo this is service. If your command line program is just a monitor for this continuously running process, closing it would have no effect upon the underlying process.


In reply to Re: Window Close / Process Termination Handler on Windows by Marshall
in thread Window Close / Process Termination Handler on Windows by rminner

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.