• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

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

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
Ehsun
Asked:
Ehsun
  • 5
  • 4
  • 3
  • +2
1 Solution
 
TheRealLokiSenior DeveloperCommented:
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
 
EhsunAuthor Commented:
No I don't have enough knowledge about that, Please axplain more about capturing wm_message.
0
 
JustinWillisCommented:
I would like to know this also so that my tray app saves data properly before windows shutdown.

Regards,
Justin Willis.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
ezraaCommented:
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
 
JustinWillisCommented:
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
 
EhsunAuthor Commented:
But this procedure doesn't work for me. I use it in Winodws XP and develop my program with Delphi 7!
0
 
JustinWillisCommented:
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
 
ZhaawZSoftware DeveloperCommented:
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
 
ezraaCommented:
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
 
TheRealLokiSenior DeveloperCommented:
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
 
EhsunAuthor Commented:
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
 
TheRealLokiSenior DeveloperCommented:
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
 
JustinWillisCommented:
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
 
TheRealLokiSenior DeveloperCommented:
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
 
JustinWillisCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 5
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now