Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB application prevents Windows shutdown.

Status
Not open for further replies.

dramsden

Programmer
Nov 28, 2003
5
GB
Hi all,

I'm pretty sure my VB application is preventing Windows from shutting down etc.
As I've not had this problem before I started to run my application I wrote in VB6.

It sits in the taskbar (uses standard shell APIs for this). Every X seconds, it uses FindWindow API to check for a window title. If it's there, grabs the window title text.
It also uses WinSock object and Inet object (but not constantly sending/recieving data).

As I said, I do Start -> Shutdown and select "Shutdown".. things like Messenger close and then everything stops.
I'm 98% sure it's my VB6 application that's causing this.

Can anyone help me out?
 
D'oh - Not having a very clever day!
I know what it is...

Globally, I have a bool called bUnload. It's set to False by default. It's only set to True if the user clicks the File -> Exit option in my menu.
This then allows the application to close.

Otherwise, if bUnload is False in QueryUnload it does:
Me.WindowState = vbMinimized
Me.Hide
Cancel = -1

So clicking the X for example minimises the application to system tray rather than closing it - Which is what I want.

Obviously, this is prevent Windows from shutting down, restarting etc. as bUnload will be false when a termination signal is sent to the application.

Any ideas how I can "intercept" such a signal so I can then set bUnload to True to allow the application to close down and thus allowing Windows to shutdown, reboot or whatever?

Thanks in advance!
 
could you add code to the cancel exit sub so that the app only terminates if it is currently in the minimised state in the system tray?

You just need to make sure that you do not have a menu with exit as an option when you right click on the icon in the sys tray.

BB
 
I think I've got it sorted now, thanks to Tek-Tips (managed to find a simular question).

Here was my old Form_QueryUnload() code:
--cut
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
' Only terminate if bUnload flag is true - This only occurs if the user
' selects "Exit" from the File menu.
If bUnload = True Then
Timer.Enabled = False

' Get rid of system tray icon
try.cbSize = Len(try)
try.hwnd = Me.hwnd
try.uId = 1&
Shell_NotifyIcon NIM_DELETE, try

' End (needed because doesn't always exit cleanly due to system tray)
End
Else
' Otherwise they've clicked the X button and we're transforming this
' so it minimises the client.
Me.WindowState = vbMinimized
Me.Hide
Cancel = -1
End If
End Sub
--cut

You can see clearly the bUnload use. As I said, it's set to True only if the user exits via File -> Exit in my application.
This obviously didn't take account for Windows session termination, Task Manager etc.

So here is the new Form_QueryUnload() code:
--cut
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim i As Integer

' Allow unloading in special cases
Select Case UnloadMode
Case vbAppWindows ' Windows session ending
bUnload = True
Case vbAppTaskManager ' Task Manager close
bUnload = True
Case vbFormCode ' Unload statement from code
bUnload = True
End Select

' Only terminate if bUnload flag is true - This only occurs if the user
' selects "Exit" from the File menu.
If bUnload = True Then
Timer.Enabled = False

' Get rid of system tray icon
try.cbSize = Len(try)
try.hwnd = Me.hwnd
try.uId = 1&
Shell_NotifyIcon NIM_DELETE, try

' Close all forms correctly
For i = Forms.Count - 1 To 0 Step -1
Unload Forms(i)
Next i

' End (needed because doesn't always exit cleanly due to system tray)
End
Else
' Otherwise they've clicked the X button and we're transforming this
' so it minimises the client.
Me.WindowState = vbMinimized
Me.Hide
Cancel = -1
End If
End Sub
--cut

As you can see, it allows for unloading in "special cases" and as an extra measure goes through all the forms and does an Unload of them (as I know this can cause problems from previous experiences!).

Thanks once again and I'm pretty sure this'll fix it but you'll soon hear from me if it doesn't :)

Regards.
 
Ah - very good. Thanks for posting your findings. I may have just the place in a current project for that.

Cheers

BB
 
Yeah thanks for posting the solution. It may come in very useful for other members who find this thread.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top