?
Solved

Delphi Application Shutdown Problems

Posted on 1998-10-15
19
Medium Priority
?
640 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
[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
  • 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
Industry Leaders: 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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 this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

764 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