Solved

Shutdown XP and OnCloseQuery problem.

Posted on 2006-07-18
5
264 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
Comment Utility
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
Comment Utility
>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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

11 Experts available now in Live!

Get 1:1 Help Now