1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Windows will not shut down.

Discussion in 'Questions (Windows Mobile)' started by RandomCoder, Sep 17, 2008.

  1. RandomCoder

    RandomCoder Well-Known Member Licensed 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?

  2. Erel

    Erel Administrator Staff Member Licensed User

  3. RandomCoder

    RandomCoder Well-Known Member Licensed User


    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.

  4. Erel

    Erel Administrator Staff Member Licensed User

    Try to work with this library: http://www.basic4ppc.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.
  5. RandomCoder

    RandomCoder Well-Known Member Licensed 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?

  6. RandomCoder

    RandomCoder Well-Known Member Licensed 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:

  7. Erel

    Erel Administrator Staff Member Licensed User

    I don't think so. The problem will only occur if you always cancel the closing process with Form.CancelClose.
  8. agraham

    agraham Expert Licensed 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: Sep 26, 2008
  9. RandomCoder

    RandomCoder Well-Known Member Licensed 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 :(

  10. agraham

    agraham Expert Licensed 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:
  11. RandomCoder

    RandomCoder Well-Known Member Licensed User

    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:

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

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

  12. agraham

    agraham Expert Licensed User

    Just have a Click event in your code, it doesn't have to do anything.
    Sub NotifyIcon_Click
    End Sub
  13. RandomCoder

    RandomCoder Well-Known Member Licensed User

    Did I say already that YOU'RE A STAR!

    Thanks, your help has been very appreciated.

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice