shutting down an vb exe  when OS shuts down

Posted on 2002-07-14
Last Modified: 2010-05-02

i have an exe running which does some task after each 15 mins. i am using the Sleep API for the appl to sleep for the specific time period. as the exe would be running for the whole day, i would like that it should automatically close when the OS is shutting down (mostly it would be Windows NT,98 or 2000). right now it gives the "appl not responding error" while shutting down.

i actually want to know if some event is generated when the appl is closed using the task manager. i have alreaday tried the Query_Unload event.

plz help..............

Question by:navneetha
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7153582
The Query_Unload should work, but maybe your code isn't complete or doesn't unload correctyl.
I assume you have a single form (invisble), maybe the form is not loaded at all to recieve the Query_unload event. You might check the load and unload events (by writing a small line to a debug file) to monitor these events...


Accepted Solution

AlonHirsch earned 50 total points
ID: 7153694

When you use the Sleep API - your app stops responding for the entire duration it is sleeping - it does not use any CPU cycles.
To circumvent this - you need to sleep for shorter periods (maybe 10 seconds at a time) and then use a Doevents.
This will ensure that your program does checks for activity etc. every 10 seconds and then becomes dormant again.


Expert Comment

ID: 7153872
you could use wait for WaitForMultipleObjects....
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


Expert Comment

ID: 7153908
Hi Sabnis,

What objects would you wait for ?


Expert Comment

ID: 7154441
wait for handle of the Thread Stop Event .... the timer elapsed event ....these are the 2 events necessary...may be other events as u need

Expert Comment

ID: 7154454
wait for handle of the Thread Stop Event .... the timer elapsed event ....these are the 2 events necessary...may be other events as u need

Expert Comment

ID: 7154831
navneetha, if you are sure that the form is loaded, and that you use "sleep" function with small intervals. I think the problem may be in "End" statement, use this code instead:

'In you main module
Public Const ForRelease = True
Public Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, ByVal uExitCode As Long) As Long
Public Declare Function GetCurrentProcess Lib "kernel32" () As Long

'Add this code instead of End statement
If ForRelease Then
  Dim ExitCode As Long
  TerminateProcess GetCurrentProcess, ExitCode
End If

The problem in this code is that it will terminate VB entirley if you run your program from VB, and so to avoid this problem, change the value of 'ForRelease' to 'False', and before you create your exe file, change it into 'True'.

LVL 49

Expert Comment

ID: 8012179
Hi navneetha,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept AlonHirsch's comment(s) as an answer.

navneetha, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.
DanRollins -- EE database cleanup volunteer

Expert Comment

ID: 8095576
Comment from expert accepted as answer

E-E Admin

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

805 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