Board index » cppbuilder » WM_ENDSESSION + Win2k/WinXP

WM_ENDSESSION + Win2k/WinXP

I have some apps making trouble since they are
running under Win2k/WinXp. No problem with Win98/WinME.

When I receive the WM_ENDSESSION message I make all
the cleanup necessary before returning from this message, but
under Win2K/WinXP this cleanup process does not come to
the point where to return from the procedure but it is interrupted
in between by Windows. This is not a problem under Win9x/WinME.

Imortant to say that the apps which are affected are running in the system
tray
that means the TApplication's window is hidden. If the app is restored
from the system tray and is fully visible when system shutdown is initiated
the problem does not occur.

Any comments what the cause of this problem is, or suggestions for
work around the problem are welcome.

Thank's.
Frank.

 

Re:WM_ENDSESSION + Win2k/WinXP


Quote
"Frank Gruber" <HVFDMIPUM...@spammotel.com> wrote in message

news:3ef08c31$1@newsgroups.borland.com...

Quote
> When I receive the WM_ENDSESSION message I
> make all the cleanup necessary before returning from
> this message, but under Win2K/WinXP this cleanup
> process does not come to the point where to return
> from the procedure but it is interrupted in between by
> Windows. This is not a problem under Win9x/WinME.

If I remember correctly, I think 2K/XP has a timeout period for that
message.  If you do not return control back to the OS in a timely fashion,
the OS moves on anyway and assumes your application stopped responding.  Try
utilizing the WM_QUERYENDSESSION message as well, do some of your cleanup
work there as well, so that WM_ENDSESSION has less work to do and can return
back to the OS faster.

Gambit

Re:WM_ENDSESSION + Win2k/WinXP


Quote
> If I remember correctly, I think 2K/XP has a timeout period for that
> message.  If you do not return control back to the OS in a timely fashion,
> the OS moves on anyway and assumes your application stopped responding.
Try
> utilizing the WM_QUERYENDSESSION message as well, do some of your cleanup
> work there as well, so that WM_ENDSESSION has less work to do and can
return
> back to the OS faster.

That seems not to be entirely correct.
If the timeout is reached Windows displays a messagescreen where you are
informed
that the app in question is not responding and you are asked to cancel it.
But this
message is not displayed in my app. If I put a Sleep(10000) as first
statement
in the WM_ENDSESSION eventhandler I get this messagescreen. If I remove this
line
I get nothing and the app is terminated quite fast but the last statements
in the
WM_ENDSESSION event handler are not reached. It also does not explain why
all works correct if the app is not hidden. It seems that one of the actions
I perform
directly or indirectly in the event handler makes Windows immediately
interrupt it.

It is getting even better. As shutdown is difficult to debug I put a fopen,
sprintf, fclose
sequence after every action I perform in the event handler to find out where
the
handler is getting interrupted. With these lines inside the message handler
it is no longer
interrupted by Windows. If I remove these lines the problem comes back.

IMHO the whole Windows shutdown process was allways full of quirks, badly
designed
or at least badly dokumented. Just my 2 cents.

Everybody who has comments or experiences with Windows shutdown is
kindly invited to this discussion.

Thank's.
Regards,
Frank.

Re:WM_ENDSESSION + Win2k/WinXP


Quote
> Everybody who has comments or experiences with Windows shutdown is
> kindly invited to this discussion.

I just got through rewriting a bunch of code because at window logoff, my
dtor
in my main form wasn't getting called.  With some help from Remy and a
couple
of others we saw that at logoff, the OnClose event wasn't firing.  I think
it had something to do with the
WM_ENDSESSION  message.  I worked around this by putting everything in the
OnCloseQuery
event instead.  Not sure if it's the same problem but it sounds similar...

Re:WM_ENDSESSION + Win2k/WinXP


Quote
> of others we saw that at logoff, the OnClose event wasn't firing.  I think
> it had something to do with the
> WM_ENDSESSION  message.  I worked around this by putting everything in the
> OnCloseQuery
> event instead.  Not sure if it's the same problem but it sounds similar...

Not exactly. The WndProc of the TApplication object intercepts the
WM_ENDSESSION
message and sets FTerminate to true so that the message loop is left after
returning from
this message. The problem is that this will never happen because as soon as
you return
from the WM_ENDSESSION Windows will terminate the app. This is the design as
stated in the Windows docs and so the VCL code will never work. But in my
case
the WM_ENDSESSION message handler is interrupted by Windows before it
returns,
and that is a different story.

Thank's for sharing your experiences.
Regards,
Frank.

Re:WM_ENDSESSION + Win2k/WinXP


Quote
"Frank Gruber" <HVFDMIPUM...@spammotel.com> wrote in message

news:3ef0a332@newsgroups.borland.com...

Quote
> If the timeout is reached Windows displays a
> messagescreen where you are informed that the
> app in question is not responding and you are
> asked to cancel it.

Not during a system shutdown.  Everything must terminate during a shutdown,
so if an app stops responding, the OS just terminates it.  At least, that is
my understanding of how things work.

Quote
> If I remove this line I get nothing and the app is terminated
> quite fast but the last statements in the WM_ENDSESSION
> event handler are not reached

That also suggests to me that the OS is actually force-terminating the
process after a period of time.

Gambit

Re:WM_ENDSESSION + Win2k/WinXP


Quote
Frank Gruber wrote:
> It is getting even better. As shutdown is difficult to debug I put a fopen,
> sprintf, fclose
> sequence after every action I perform in the event handler to find out where
> the
> handler is getting interrupted. With these lines inside the message handler
> it is no longer
> interrupted by Windows. If I remove these lines the problem comes back.
> IMHO the whole Windows shutdown process was allways full of quirks, badly
> designed
> or at least badly dokumented. Just my 2 cents.

You just need to read MSDN. In some topic (don't have MSDN now) there's
a statement saying something like this: "After this message is sent, any
API operation can fail with ERROR_SHUTDOWN_IN_PROGRESS error". And this
is what happens. Actually, you can't trust OS even after
WM_QUERYENDSESSION. So all shutdown actions you want to do should be
done in responce to WM_QUERYENDSESSION. The best thing is to close the
application by posting WM_CLOSE to main form when you receive
WM_QUERYENDSESSION and not wait until system kills your application. In
ElPack (http://www.eldos.org/elpack/elpack.html) we have a special unit,
whose only job is to track WM_QUERYENDSESSION and gracefully close the
application. This unit is also present in my article on Delphi3000.

--
Eugene Mayevski
Flexible cross-platform information exchange framework
http://www.msgconnect.com/

Re:WM_ENDSESSION + Win2k/WinXP


Quote
> You just need to read MSDN.

That's what I have done so far.

Quote
> In some topic (don't have MSDN now) there's a statement saying something
> like this: "After this message is sent, any API operation can fail with
> ERROR_SHUTDOWN_IN_PROGRESS error".

I was searching for this but could not find it. This is not stated by the
messages itself.
If you have a reference to this statment it would be nice if you share it
with us.

Quote
> The best thing is to close the application by posting WM_CLOSE to main
> form when you receive WM_QUERYENDSESSION and not wait until
> system kills your application.

We will give this a try.

Regards,
Frank

Re:WM_ENDSESSION + Win2k/WinXP


The only thing I found by reading the MSDN is the following:

Searching the MSDN shows a different meaning for that
error code. That's what I found:

InitiateSystemShutdown
"The error ERROR_SHUTDOWN_IN_PROGRESS is returned
if a shutdown is already in progress on the specified computer."

Are you sure tjat you do not mix up anything ?

Thank's for your help.
Regards,
Frank.

Re:WM_ENDSESSION + Win2k/WinXP


Quote
> > The best thing is to close the application by posting WM_CLOSE to main
> > form when you receive WM_QUERYENDSESSION and not wait until
> > system kills your application.

We tried this solution but it does not work as desired. Maybe we did
something wrong or misunderstood how it should work. In this case the
app is also killed before it is able to leave the message loop and
cleanly close anything. We also tried to post WM_QUIT from within
WM_QUERYENDSESSION but with the same result.

Thank's again for your suggestions.
Kind regards,
Frank.

Re:WM_ENDSESSION + Win2k/WinXP


Quote
Frank Gruber wrote:
>>In some topic (don't have MSDN now) there's a statement saying something
>>like this: "After this message is sent, any API operation can fail with
>>ERROR_SHUTDOWN_IN_PROGRESS error".
> I was searching for this but could not find it. This is not stated by the
> messages itself.
> If you have a reference to this statment it would be nice if you share it
> with us.

Sorry, I don't have MSDN at home :(

But it definitely was there and this is why we created ElShutdownMonitor
(the one that is in Delphi3000 article).

--
Eugene Mayevski
Flexible cross-platform information exchange framework
http://www.msgconnect.com/

Re:WM_ENDSESSION + Win2k/WinXP


Quote
"Eugene Mayevski" <mayev...@eldos.org> wrote in message

news:3ef222f7@newsgroups.borland.com...

Quote
> Sorry, I don't have MSDN at home :(

You don't have Internet access at home?  All of the latest up-to-date info
is available at http://msdn.microsoft.com

Gambit

Re:WM_ENDSESSION + Win2k/WinXP


Quote
Remy Lebeau (TeamB) wrote:
>>Sorry, I don't have MSDN at home :(
> You don't have Internet access at home?  All of the latest up-to-date info
> is available at http://msdn.microsoft.com

Why bother searching in internet when one can do this on local computer?
I wonder why does anyone use msdn.microsoft.com at all ...

--
Eugene Mayevski
Flexible cross-platform information exchange framework
http://www.msgconnect.com/

Re:WM_ENDSESSION + Win2k/WinXP


Quote
"Eugene Mayevski" <mayev...@eldos.org> wrote in message

news:3ef23057@newsgroups.borland.com...

Quote
> I wonder why does anyone use msdn.microsoft.com at all ...

I use it all the time myself.

Gambit

Re:WM_ENDSESSION + Win2k/WinXP


Quote
> Why bother searching in internet when one can do this on local computer?
> I wonder why does anyone use msdn.microsoft.com at all ...

Because it is allways up to date.

Back to the topic. I was not able to find anything related to
ERROR_SHUTDOWN_IN_PROGRESS in the context you
stated. I also searched the usenet archive via google but no success.

However the solution you stated does not lead to success. Maybe
this is because we misunderstood something or implemented something
different. I'm still searching for a reliable solution to properly terminate
an application on system shutdown which also does not completely
break OOP design. We had worked out a solution a few years ago
which was reliable under Win95/98/ME but it sometimes fails under
W2k/WXP.

Maybe you are willing to give us more details of the class you
are using so that we can test your method against our cases where other
methods of proper app termination failed.

Anybody who is also interrested is kindly invited to this discussion.

Regards,
Frank.

Go to page: [1] [2]

Other Threads