?
Solved

Shutdown XP and OnCloseQuery problem.

Posted on 2006-07-18
5
Medium Priority
?
286 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 1800 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 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month13 days, 2 hours left to enroll

777 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