Solved

Delphi Application Shutdown Problems

Posted on 1998-10-15
19
629 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 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