3

using Delphi5.

I have an app that starts with windows boot and I would like to Close and Start it on the Hibernate/Wake command.

I need to be able to detect if it is coming back from an Hibernate so I can run my app each time. I have a setting in the Registry where the User can choose to run only once per day.

I am guessing there is a windows Message or Registry entry that tells the machine that it is going into and coming back from an Hibernate.

Thoughts and suggestions?

Thanks for looking at this question.

user2445336
  • 53
  • 1
  • 5
  • 1
    Why do you want to do this? Your program will not execute when the machine is sleeping. – David Heffernan Jun 02 '13 at 16:51
  • 3
    @DavidHeffernan, Where does it state that his trying to do that? He says in first paragraph that he would like to Close it on hibernate and start it again on wake up. – Peter Jun 02 '13 at 16:59
  • @Peter Yes, that's my point. Why close the app when it won't be running. If you just leave it to run everything will be fine. Why on earth would you want to close and restart the app, when you can do nothing? – David Heffernan Jun 02 '13 at 17:42
  • 2
    @DavidHeffernan, Why doesn't matter, you don't know what his intentions are. The question clearly states what he wants to know, leave your personal opinions out of it. – Peter Jun 02 '13 at 17:53
  • @Peter I don't know what the intentions are. And that's why I asked the question to find out. I said, "Why do you want to do this?" In my experience, it's often useful to understand the reason behind the question. Understanding the motivation can help give the asker the best possible answer. Also, I cannot imagine why anyone would ever want to do what is being requested here. So perhaps there's something for me to learn. If the asker will explain why, then I've got an opportunity to learn something. Such opportunities are what motivates me to spend time on the stack. – David Heffernan Jun 02 '13 at 18:02
  • I see your point, but if we don't know what his intentions are then why the statement that followed your question? Anyway, in my opinion I believe that he doesn't know exactly how to make his application data persist through the hibernation period. When Windows broadcasts that message, AFAIK gives out a maximimum of 2 seconds grace period in which apps have time to save data before the system enters Hibernation. I can only assume that his trying to close the app before Hibernation as to trigger the onDestroy/Close event. But I can only assume that's what lead him to asking this question. – Peter Jun 02 '13 at 18:20
  • Geeze, didn't mean to start a range war, but Peter Vonča is correct, it should not matter why. Obviously Dave you have not thought enough about this. It is a "thing to do" app. It runs on start up and reminds what I have to do today. If there is nothing then I close it. If there is something and I want a reminder then I leave it running. I think it is called getting to software to do what I want. Reading your posts David, you do seem to be a little autocratic. – user2445336 Jun 02 '13 at 21:06
  • @user2445336 I still don't understand why you need to close when the machine sleeps. As far as I can tell, your app will work perfectly well if it does not close. So your trying to do more work that you need to, as far as I can tell. In fact I don't understand how you can get your app to start up again upon waking if you close it on sleep. Once you've closed it, how will it be able to receive notifications that the machine has come back from sleep? – David Heffernan Jun 02 '13 at 22:27
  • "Obviously you have not thought enough about this." What do you mean? I have thought about it and hence the questions. I have a curious mind and I'm trying to understand. That's all. Apparently I don't understand your motivation and so I'm trying to work out what I am missing. Which is why I asked. – David Heffernan Jun 02 '13 at 22:29

1 Answers1

6

You could achieve such application behaviour by listening for the WM_POWERBROADCAST message in your application. This message is sent to all running applications notifying that a power-management event has occurred. wParam part of this message is where you can check if the system is entering a suspended state(PBT_APMSUSPEND) or recovering from one(PBT_APMRESUMESUSPEND).

Peter
  • 2,977
  • 1
  • 17
  • 29
  • 1
    I listed it as an option, it's not the only option, but it is an option. I disagree with your input but I welcome it anyway. – Peter Jun 02 '13 at 17:52
  • Your option 2 is the correct answer. That's all that you need. If you removed option 1 I would convert my downvote into an upvote. – David Heffernan Jun 02 '13 at 18:05
  • I've decided that I'm going with your suggestion, I do agree that in this specific case the use of tasks is probably too far fetched but I wanted to list it as an option anyway. – Peter Jun 02 '13 at 18:30
  • 1
    @Peter Vonča You should not have capitulated. Fortunately for the Editing system here I was able to see both of your suggestions and while I have to agree with David, the Task approach is overkill, **I** think I should be the one to decide since I know exactly what I want and do not want. As mentioned above, "autocratic" is the word that comes to mind here. It is a pity, as David's comments to other's questions have helped to me in the past. I don't want to alienate him from answering my stuff, but Peter, you were correct on all fronts with this. – user2445336 Jun 02 '13 at 21:16
  • "I think I should be the one to decide." Stack Overflow is a community. You might ask the question, but it doesn't belong to you. It belongs to the community. Anyway, thinking more about this, I'm beginning to regret being so hard on tasks. If you really do kill your process on sleep, I can't see anything better than tasks to start it up again. You need a process to respond to wake up, and since you killed your process, you'll need either a system process, or one of your own. That is if you do decide that termination on sleep is the best course of action! ;-) – David Heffernan Jun 02 '13 at 22:44
  • Yes, you should not be blackmailed so easily, Task Scheduler proposal fits OP's requirement perfectly (and MS does lots of internal stuff this way in 6.x versions). – OnTheFly Jun 05 '13 at 05:51