Solved

Shutdown XP and OnCloseQuery problem.

Posted on 2006-07-18
5
271 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
  • 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

825 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