Solved

Shutdown XP and OnCloseQuery problem.

Posted on 2006-07-18
5
276 Views
Last Modified: 2010-08-05
Hi.

I'm using this code to let XP shutdown normal when my program is running:
...
...
  private
    { Private declarations }
    procedure WMQueryEndSession(var Message: TMessage);
    message WM_QUERYENDSESSION;
...
...
procedure TForm1.WMQueryEndSession(var Message: TMessage);
begin
Message.result := 1;
Tag := 1;
Application.Terminate;
end;
...
...

~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
The code was working fine, till now.                                                                                      
I've add many things to the code and i dont know what exactly goes wrong...
I've tried to find out why, by disabling portions of code that seemed suspicious with no success.
The program close succesfully but windows stop shutdown as soon as my app terminates.

Can anyone help ?

Thanks in advance.
0
Comment
Question by:CodedK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 450 total points
ID: 17134875
Hard to tell without all the code, but you could try the following (also note, the query end session is just that, a query as to if the application can end at this point. It is not a good idea to terminate at this point, because another app might stop the end session process. You should perform the cleanup / shutdown in the WM_ENDSESSION message if the wParam is not zero)

Hope this helps,
Russell

---

  private
     // private declarations
     procedure   WMQueryEndSession(var Message: TMessage); message WM_QUERYENDSESSION;
     procedure   WMEndSession(var Message: TMessage); message WM_ENDSESSION;
  public
     // Public declarations
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

procedure TForm1.WMQueryEndSession(var Message: TMessage);
begin
  Message.result:=1;
  Tag:=1;
end;

procedure TForm1.WMEndSession(var Message: TMessage);
begin
  if (Message.wParam <> 0) then Close;
end;
0
 
LVL 17

Assisted Solution

by:TheRealLoki
TheRealLoki earned 50 total points
ID: 17135342
>I've add many things to the code
Was 1 of the things you added, a TrayIcon?
If so, you may need to disable it manually before you close your application.
0
 
LVL 16

Author Comment

by:CodedK
ID: 17136665
Hi, thanks for the replies.

>>Was 1 of the things you added, a TrayIcon?
Tray icons existed before the problem.
I've added :

    FMsgTaskbarCreated : dword;
    procedure WndProc(var Message: TMessage); override;

To recreate the icons if explorer crash. I've removed the code with no success.
OnDestory i free the tray icons, something that also existed before the problem.
---------------------------------------------------------------------------------------------
Russell I've tried your code, with no success. It doesnt go in WMEndSession.
On onclosequery i've inserted :

If Tag=1 then
begin
 ShowMessage('Can close = True');  
 CanClose:=True;
end else //just minimize the form
begin
 CanClose:=False;
 Form1.Hide;
end;

It shows the message "Can close = True" but the form doesnt close, the app works like nothing happened.

>>Hard to tell without all the code
I didnt add something that could interfere with shutdown...
Just some Try... Except statements to handle some possible errors and the recreate icons if explorer crash.


0
 
LVL 16

Author Comment

by:CodedK
ID: 17136727
The 4 procedures for Shutdown....

//--------------------------|
//   Turn off Windows    |
//--------------------------|
procedure TForm1.WMQueryEndSession(var Message: TMessage);
begin
Tag := 1;
Message.result := 1;
end;

procedure TForm1.WMEndSession(var Message: TMessage);
begin
  if (Message.wParam <> 0) then
  begin
   Tag := 1;
   Close;
  end;
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
 If Tag=1 then CanClose:= True else
 Begin
  CanClose:=False;
  Form1.Hide;
  Form4.Close;
  Form3.Close;
 end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
// disable timer
  Timer0.Enabled:=False;

// destroy tray icons
  Shell_NotifyIcon(NIM_DELETE,@F1Tray);
  Shell_NotifyIcon(NIM_DELETE,@F2Tray);

// free real icons
  F1Icon.Free;
  F2Icon.Free;
end;

//--------------------------------------------------
0
 
LVL 16

Author Comment

by:CodedK
ID: 17137137
Ok...

The same things applied for Form2, but i didnt add "WMEndSession".
Now it works :)

Thank you Russell :)
Thank you TheRealLoki too (You were right, but i've allready foreseen the tray icons issue)

By the way Russell do you know if i can get Router uptime with GetAdaptersInfo ?
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21922848.html
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

733 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