Solved

Delphi Application Shutdown Problems

Posted on 1998-10-15
19
614 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
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…

758 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

19 Experts available now in Live!

Get 1:1 Help Now