Windows will not shut down.

RandomCoder

Well-Known Member
Licensed User
Longtime User
I'm leaving work in a fews weeks time and as a small leaving gesture I've wrote a program that counts down the days, hours, minutes and seconds for my colleagues.

Only thing is, I've made this program start automatically using the windows registry, placed the form on top of all other windows, hidden the program from task manager and cancelled the form closing event... which instead hides the form so that I can remind my colleagues of the remaining time two hours later ;)
All is working beautifully, I've even made my program display a farewell message on the day/time of my departure and on the next couple of days at work I provide a little advice before giving them a button that will remove the registry entry and close the program for good (I'm still chuckling to myself :sign0013:)

Now for the problem - I tested everything and it all appeared to work, the messages display on the correct days and times, the window looks to have closed but returns two hours later etc. However Windows will no longer shutdown :sign0161: Instead I need to stop the process in task manager before starting the Windows shutdown. I'm guessing that I need to make my program look for a Windows message? Which one? Can someone help?

Regards,
RandomCoder.
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Erel,

I've tried Dzt's program but the source code produces an error on line 19, however the executable runs just fine.
Could this be a compatibility problem now that I'm using version 6.3?

Can you also please expand on your solution as I don't completely understand. I was expecting to be told that I should look for a Windows Message within my tick event to signify that windows wanted to shutdown. I was very surprised that my Form is keeping windows from shutting down properly, it doesn't even bring up a message asking the user if they wish to 'end task' which is what would normally happen when a program fails to close as windows shutsdown.

Regards,
RandomCoder
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try to work with this library: http://www.b4x.com/forum/additional-libraries/384-useful-library-2.html#post2809
When windows sends the close message to your application, the close event runs and cancels the request.
I assume that at this time explorer.exe no longer runs (you need to check it).
So you can add a condition in the close event sub and check whether this process exists or not.
If it doesn't exist then don't cancel the application.
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Still no joy.

Erel, I now understand what you meant me to do but unfortunately explorer.exe is still running, presumably because windows is attempting to shutdown my app first (which it can't).

I've found this MSDN article which I think will help, WM_ENDSESSION Message (Windows) but I lack the knowledge to fully understand and implement it.
Surely this isn't the first app to be written that runs in the background and needs to shut automagically :(
Can anyone help?

Regards,
RandomCoder
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
I'm still struggling to get my app to close correctly when the user wants to shutdown windows.
Using DZT's EventMagic library I have hooked the WM_QueryEndSession (decimal 11) and WM_EndSession (decimal 22) and with this I've been able to close the app but windows still does not shutdown until requested to do so a second time.
I think that this is because I need to return a value when I recieve either of the EndSession messages, but how to do this?

Can someone please help?

This has implications for another program I am writing which schedules file backups, as it is also running in the background all of the time I suspect that it will also prevent windows from shuttingdown correctly :sign0161:

Regards,
RandomCoder
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This has implications for another program I am writing which schedules file backups, as it is also running in the background all of the time I suspect that it will also prevent windows from shuttingdown correctly
I don't think so. The problem will only occur if you always cancel the closing process with Form.CancelClose.
 

agraham

Expert
Licensed User
Longtime User
WM_QueryEndSession (decimal 11) needs a return value of "true" to carry on shutting down. What are you setting for the value of ReturnHandled in the dzEventsMagic constructor (New1)? I have looked inside the dll and if it is true then it looks as though true is returned to Windows. If it is false the old WindowProc is called, which should also in turn return true to Windows. As a long shot try setting ReturnHandled to the opposite of whatever you are using.

EDIT:- I just noticed ReturnHandled is available as property, if not set explicitly in your Event code then the default is what was set in the constructor. Anyway, whatever you are doing try the opposite.
 
Last edited:

RandomCoder

Well-Known Member
Licensed User
Longtime User
Still having problems but....

I've kept playing with this, and mostly gone round in circles.
Anyhow, I decided to make a small demo app to show the problem that I am experiencing.
Only thing is, it worked perfectly :confused:
Then I realised that I'm using Agraham's FormExDesktop library in my app because I use some of the extra functionality it offers.

Here is where the problem lies...
When using the FormExDesktop library my app no longer detects the WM_QUERYENDSESSION message although it does see the WM_ENDSESSION message.
If I use a normal Form with the Door library all works as it should.

Can anyone shed some light on this?
Attached is my demo app using Agrahams library, you need to also add the FormLib, ControlsExdesktop,dzEventsMagic and FormExDesktop libraries.
The Demo.exe is a complied normal Form using the door library and Demo1.exe is using an extended Form (for some reason I get an error when trying to show the Form using the notifyicon - I don't understand why!)

So just to confirm, the problem is that my app is NOT seeing the WM_QUERYENDSESSION although it does catch the WM_ENDSESION :(

Regards,
RandomCoder
 

agraham

Expert
Licensed User
Longtime User
You are using the wrong form. Frm is your donor form, the form in use is FrmProperties. New it first then hook the messages changing your references to "Frm" to FrmProperties.ControlRef. I'm a bit surprised you are getting any messages as I thought there wouldn't be a message loop running on Frm.

Why the references to the Door library? :confused:

The NotifyIcon probably errors because you don't have a Click event for it. This is a bug in ControlsExDesktop that I have not noticed before :sign0013:
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
You are using the wrong form. Frm is your donor form, the form in use is FrmProperties. New it first then hook the messages changing your references to "Frm" to FrmProperties.ControlRef. I'm a bit surprised you are getting any messages as I thought there wouldn't be a message loop running on Frm.

Agraham, YOU'RE A STAR :sign0188:, I had previously tried hooking the Windows Messages with FrmProperties but this produced errors. My inexperience is shining through as I had not used the ControlRef property but now it works! :sign0060:

Why the references to the Door library? :confused:

I used the door library to access the visible property when using a 'normal' Form.


The NotifyIcon probably errors because you don't have a Click event for it. This is a bug in ControlsExDesktop that I have not noticed before :sign0013:

Any idea how I can stop this or is it just a case of using an ErrorLabel and then ignore the error?

Regards,
RandomCoder
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Did I say already that YOU'RE A STAR!

Thanks, your help has been very appreciated.

Regards,
RandomCoder
 
Top