• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

Urgent-Terminate my program when windows logoff/shutdown event occurs

Hi dudes

I have a program that sits in the background and waits for certain events to occur. The program works fine, only until the user tries to log off or shutdown the system. Whenever the logoff/shutdown event occurs, a message box pops up saying that "XY Program Not Responding .... End Now?". However I want to capture the logoff event in my program, so that I can end my program as soon as the logoff event is initiated.
An immediate help would be appreciated.

Thanks in advance
0
cool_dudeps
Asked:
cool_dudeps
1 Solution
 
DocMCommented:
Hi,
Check the value of the UnloadMode property in the Form_QueryUnload event.

From VB help:

The unloadmode argument returns the following values:

Constant  Value Description

vbFormControlMenu 0 The user chose the Close command from the Control menu on the form.

vbFormCode  1 The Unload statement is invoked from code.
vbAppWindows  2 The current Microsoft Windows operating environment session is ending.

vbAppTaskManager 3 The Microsoft Windows Task Manager is closing the application.

vbFormMDIForm  4 An MDI child form is closing because the MDI form is closing.
0
 
VKCommented:
Perhaps he has no Form.
It could be (in the simplest form):

do while true
  '...some code here to detect the logoff-event
  DoEvents
loop

This should be done by subclassing the desktop window and catching the WM_CLOSE Message or someting like that.

0
 
cool_dudepsAuthor Commented:
Actually what I want is to set a very low priority of my application, so that the shutdown event forcibly terminates my program. Any suggestions????
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
wcwnetCommented:
Your app has something either looping with no way to exit the loop, if this is by design, place a variable in the loop for exit on certain condition, in the main form Exit put a line that sets the exit condition for your loop.
0
 
VKCommented:
Hello cool_dudeps !

You always will get "App not responding" from windows if your app doesn't respond to an "system logoff"-event - even if you set the prority very low.

If you want to set the prioroty of your app very low you could use a timer (with form or not)where you execute the whole background task. Set a big Intervall for low priority.

Also it is suitable to execute a DoEvents statetment periodically if windows shouldn't get "App no responding".

An easy way:

Sub main()
    Load Form1
End Sub

Private Sub Form_Load()
    Timer1.Interval = 1000
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    DoEvents
    'Do what is to do in the Background
    DoEvents
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Select Case UnloadMode
        Case vbFormControlMenu
        Case vbFormCode
        Case vbAppWindows
            'Here you will get if system will be shut down
        Case vbAppTaskManager
        Case vbFormMDIForm
        Case vbFormOwner
    End Select
End Sub
0
 
CleanupPingCommented:
Hi cool_dudeps,
This old question (QID 20567268) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.
0
 
GPrentice00Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

 -->PAQ - with points refunded

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER

GPrentice00
Cleanup Volunteer
0
 
YensidModCommented:
This question is PAQed and no points refunded (of 50)

YensidMod
Expert Exchange Moderator
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now