Solved

Shutdown XP and OnCloseQuery problem.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Comparing dates in Delphi. Greater than/ Less than 3 117
LAN or WAN ? 11 82
Convert a string into a TDateTime 5 50
Delphi: Connect to running MS Outlook 4 48
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now