Solved

Delphi Application Shutdown Problems

Posted on 1998-10-15
19
635 Views
Last Modified: 2012-05-07
I have written a delphi 4 application and am having problems when the user chooses to ShutDown Windows.  When the Win95 shutdown/restart process is called, my application regains focus and Windows ceases to shutdown.  Any ideas?
0
Comment
Question by:PaulKorzycki
  • 5
  • 5
  • 4
  • +3
19 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1343046
Why don't you shut-down your application?
0
 
LVL 1

Expert Comment

by:Romanian
ID: 1343047
Is this console application? It's like FAR in Win95. I think Win95 don't send WM_QUIT to console application and I think you must hook WM_SHUTDOWN or WM_QUIT(I don't remember which one). Try to do it.
0
 

Author Comment

by:PaulKorzycki
ID: 1343048
The windows shutdown process stops completely (ie, I can continue to work under windows).

The reason for not shutting down manually is that the application is running in the System Tray, and is a "background" application.


0
Technology Partners: 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!

 

Author Comment

by:PaulKorzycki
ID: 1343049
Romanian, my app is not a console app, and has all of the neccessary forms.  On FormClose I do some clean up of files, but even when I remove that, it still does not work.  I can quit manually with no problems.  I am thinking that OnCloseQuery is not being called

Could this be a problem with a shareware component (I am using a serial communications component, a TrayIcon component, and a delay (sleeper) component.)

0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1343050
do you close your serial connection? And how does the delay component work?
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1343051
Hello!

The problem is I guess in the way you allow your app to close. I think you've set the CanClose := False; under the OnCloseQuery() but that's not what you should do. YOu need to use the following. You need to set Action := caNone; in the OnClose() of the form. Hope this helps!

Regards,
Viktor Ivanov
0
 

Author Comment

by:PaulKorzycki
ID: 1343052
OnCloseQuery does not contain any code (and I've tried setting canClose:=True....to no avail).  When I put Action:=caNone into the OnClose section of the form, I can't close it at all!
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1343053
It seems that my comments don't exist here? Does anybody even care of my comments?
0
 

Author Comment

by:PaulKorzycki
ID: 1343054
Sorry ZifNab:
Yes I close the serial connection using the routine in the component.  The sleeper component relies on the system timer (it is basically to put in delays in the program code).

I think it may be a problem with one of these components
0
 
LVL 1

Expert Comment

by:Asw
ID: 1343055
Try this code in the OnClose event  it may work.

PostMessage(FindWindow(Nil, 'My App'), WM_QUIT, 0, 0);
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1343056
Paul,

my suggestion:
Try to write a handler for "Application.OnMessage". For this purpose you'll have to add a new method to your main form and in "mainForm.Create" please add "Application.OnMessage:=MyAppMessageProc". In this MyAppMessageProc answer the WM_QUERYENDSESSION message. So none of your components are able to deny this message.

What Asw suggested probably won't work, since if one of your components deny the WM_QUERYENDSESSION message, the FormClose method isn't called at all. But if I'm wrong in this point you should call PostQuitMessage instead of using PostMessage(...).

Regards, Madshi.
0
 
LVL 1

Accepted Solution

by:
Romanian earned 100 total points
ID: 1343057
I've found solution!!!
You must change next code in TTrayIcon component!
----- start ---
procedure TTrayIcon.WndProc(var msg : TMessage);
var MouseCo: Tpoint;
begin
   with msg do
      if (msg = WM_RESETTOOLTIP) then
         SetToolTip( fToolTip )
      else if (msg = WM_TOOLTRAYICON) then begin
         case lParam of
            WM_LBUTTONDBLCLK   : if assigned (FOnDblClick) then FOnDblClick(self);
            WM_LBUTTONUP       : if assigned(FOnClick)then FOnClick(self);
            WM_RBUTTONUP       : if assigned (FOnRightClick)then
                                 begin
                                   GetCursorPos(MouseCo);
                                   FOnRightClick(self,mbRight,[],MouseCo.x,MouseCo.y);
                                 end;
         end;
      end
{<<<<<<<<< ADDED>>>>>>>>>}
else if (msg = WM_QUERYENDSESSION) then
                 Result := Longint(True)
               else if (msg = WM_ENDSESSION) then
                 Application.Terminate;
{<<<<<<<<< ADDED>>>>>>>>>}

end;
---Finish---
Windows with Active = false don't receive messages like WM_QUERYENDSESSION and WM_QUERYENDSESSION. So you must hadle this messages in TTrayIcon.WndProc.
Try it.
I've tested under WinNT my computer was shutdowned without any problem.

0
 
LVL 1

Expert Comment

by:Romanian
ID: 1343058
To Madshi:
If MainWindow invisible we can't receive messages by this window and code writed to handle onMessage don't work
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1343059
Romanian,

you're right. I didn't know that the main window is invisible. And handling OnMessage really doesn't work...  :-(
But in your answer there's a little bug, too. You should terminate the program only when WM_ENDSESSION comes with "wParam=true"!

Regards, Madshi.
0
 
LVL 1

Expert Comment

by:Romanian
ID: 1343060
To Madishi:

Yes, it's a little bug:) Probably I must handle not only wParam, but lParam too. So application could work if you logoff but don't shutdown computer.

I think WndProc must call DefWndProc for any other messages which not handled in this procedure. And what do you think about this?
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1343061
Romanian,

I think, it doesn't matter whether windows logs off or shuts down, because all applications are closed in both cases.
Why calling DefWndProc? Can't you just call "inherited" for messages that are not handled?

Regards, Madshi.
0
 
LVL 1

Expert Comment

by:Romanian
ID: 1343062
To Madshi:

But if I use WinNT and I need that my application work when nobody is logged in. Or I must do this via NT services?
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1343063
Romanian,

oops... don't know that. I am working with win95 only. But I know that if you're using NT, the logoff parameter of the WM(QUERY)ENDSESSION messages is always 0. That means you can't determine, whether windows logs off or shuts down.    :-(

Regards, Madshi.
0
 

Author Comment

by:PaulKorzycki
ID: 1343064
Romanian,  you were right (partly!)

The trayicon component seemed to work fine, but the communications component was the culpret.  I inserted your piece of code into the WndProc portion of the communications component, and it works now!

Good Work!!!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
can't find the executable in Simulator 1 112
Controlled Assessment GCSE - desperate help needed 4 112
DBCtrlGrid, Delphi, Scroll 8 60
shape, triangle, dbctrlgrid 3 33
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

730 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