?
Solved

I want sense when windows are going to Shut down in My program

Posted on 2005-04-16
15
Medium Priority
?
225 Views
Last Modified: 2010-04-05
I have developed a program that sit in the system tray. But  I have big problem. When My program is in the System tray, Windows XP fails to shut down. I need to find out when user wants to shut down windows and close my program at that time automaticly.

Please help me!
0
Comment
Question by:Ehsun
  • 5
  • 4
  • 3
  • +2
15 Comments
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 13800195
you need to capture the wm_QueryEndSession message and shut down your app when you receive it
This sort of problem happens frequently with apps that have a tray icon.
Let me know if you need an example of the wm_message capturing
0
 
LVL 1

Author Comment

by:Ehsun
ID: 13800647
No I don't have enough knowledge about that, Please axplain more about capturing wm_message.
0
 
LVL 1

Expert Comment

by:JustinWillis
ID: 13801131
I would like to know this also so that my tray app saves data properly before windows shutdown.

Regards,
Justin Willis.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Accepted Solution

by:
ezraa earned 200 total points
ID: 13801530
In the interface, Add this to the private section of your form:

  procedure WMQueryEndSession (var mesg: TWMQueryEndSession); message WM_QueryEndSession;

And this is the implementation:

procedure TForm1.WMQueryEndSession (var mesg: TWMQueryEndSession);
begin
//  if not CanCloseWindows then Msg.Result := 0 else
  begin
      Msg.Result := 1;
      Close; // Or you can use Halt or some other way of shutting down your application.
   end;
end;

also you can take a look here: http://www.delphicorner.f9.co.uk/tips.htm#Preventing
0
 
LVL 1

Expert Comment

by:JustinWillis
ID: 13801927
An excellent example, thanks.

Turns out though that I didn;t need this after all as I am stopping close in the OnClose using..

        action := caNone;

Rather than using OnCloseQuery, is this a bad idea? seems to be working for me ok...

Regards,
Justin Willis.
0
 
LVL 1

Author Comment

by:Ehsun
ID: 13802139
But this procedure doesn't work for me. I use it in Winodws XP and develop my program with Delphi 7!
0
 
LVL 1

Expert Comment

by:JustinWillis
ID: 13804286
Are you able to post your code or an example of what you are doing, need a bit more to go on I think..

Regards,
Justin.
0
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 13805197
ezraa, Halt procedure should not be used to close an app. It performs an abnormal termination. Closing "main form" (Close or Form1.Close or smth like that) or Application.Terminate should be used.
0
 
LVL 9

Expert Comment

by:ezraa
ID: 13807445
ZhaawZ, I know, and don't recommend the use of halt. I only suggest it as a way to test if the message is being captured correctly.

Ehsun, If you describe your problem with a little more detail, we will be able to help you.
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 13810057
If you are not closing the main form, your application will not close, did you also try

procedure TForm1.WMQueryEndSession (var mesg: TWMQueryEndSession);
begin
//  if not CanCloseWindows then Msg.Result := 0 else
  begin
      Msg.Result := 1;
      Application.Terminate;
   end;
end;

?

Try putting a "ShowMessage('beep');"  in there to make sure your application is catching the windows shutdown message
Does the message appear? Try it and let us knopw, we'll be able to help a bit more based on your answer
0
 
LVL 1

Author Comment

by:Ehsun
ID: 13812007
Thanks a lot my friends, My problem was solved. But please tell me
what does this line do?
       msg.result := 1

Thanks a lot
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 13812033
it gives you the option of telling windows that it is not ok to shutdown
a 1 means, "yes, i will close, it is ok to shutdown"
0
 
LVL 1

Expert Comment

by:JustinWillis
ID: 13813349
If I want to delay the shutdown, so that I can save data files etc and then continue with the shutdown, can I just put the saving code in this procedure (TForm1.WMQueryEndSession) or should I stop the windows shutdown, save my data and then initiate another shutdown, only problem with that is that I won't know whether it's restart or shutdown etc..

Does that make sense? Should I post this as a new question?

Regards,
Justin Willis.
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 13819149
it depends how long it takes you to save your settings. windows might eventually pop up with an end task box, although probably not in your case.
if it's only gonna take a coupl eof seconds to save, do that first, then answer the message
0
 
LVL 1

Expert Comment

by:JustinWillis
ID: 13819704
ok, would probably take less than 1 second, will try and put it in this procedure and then answer windows as you suggest.  May have to look at this again when my data files grow.

Thanks.
J
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question