|
-
Sep 22nd, 2005, 01:38 PM
#1
Windows Unable to Terminate my VB Application... WHY ???
Hi,
This is really a very unusual problem
I have an MDI application which doesn't terminate automatically when user logs off or suutdown/restart windows, thus not allowing windows to logoff/shutdown/restart when it is running.
However it can be end tasked from task manger.
As the data being shown in child forms is huge, I just hide the form instead of unloading it so that it can be shown again quickly when user requires it.
When MDI FOrm is terminating it calles a procedure which flags a global boolean that application is terminating, so all MDI children should allow unloading. The application works fine without a hitch.
Where could the problem really be?
Pradeep
Last edited by Pradeep1210; Sep 22nd, 2005 at 03:38 PM.
-
Sep 22nd, 2005, 01:46 PM
#2
Fanatic Member
Re: Windows Unable to Terminate my VB App.
When the computer is shutting down is closes all programs. Because your program is so huge it may take while to unload. It will seem longer then usual because windows is also closing all other applications. I don't know of a way to make it close quickly like the task manager does.
-
Sep 22nd, 2005, 01:52 PM
#3
Re: Windows Unable to Terminate my VB App.
 Originally Posted by Slyke
When the computer is shutting down is closes all programs. Because your program is so huge it may take while to unload. It will seem longer then usual because windows is also closing all other applications. I don't know of a way to make it close quickly like the task manager does.
No, I've observed it for quite a while.
Unless I close this app, windows refuses to shutdown.
The screen just dimm's for a second and then becomes normal again. NO harddisk activity either(which normally happens when windows is shutting down). Even if I wait for half an hour it won't shutdown.
Pradeep
-
Sep 22nd, 2005, 02:31 PM
#4
Re: Windows Unable to Terminate my VB Application
-
Sep 22nd, 2005, 03:35 PM
#5
Re: Windows Unable to Terminate my VB Application
-
Sep 22nd, 2005, 04:41 PM
#6
Re: Windows Unable to Terminate my VB Application... WHY ???
This is a guess but have you tried something like this?
VB Code:
Private Sub MDIForm_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Select Case UnloadMode
Case vbAppWindows, vbAppTaskManager
' Set your boolean and then...?
End Select
End Sub
BTW, bumping threads is frowned on, especially only afrer an hour or so.
-
Sep 23rd, 2005, 01:18 PM
#7
Re: Windows Unable to Terminate my VB Application... WHY ???
Thanks a lot Martin.
I'll try that tommorrow in my office. (the app is for office internal use)
BTW
The whole application is too big to be posted here. So here is just the relevent parts of the application. Hope this would give more insight about the problem:
VB Code:
'****** MDI Children Forms ******************
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If Not (UnloadMode = vbFormMDIForm Or bForceUnload) Then
Cancel = True
Me.Hide
End If
End Sub
'****** MDI Parent Form ******************
Private Sub MDIForm_Unload(Cancel As Integer)
On Error GoTo ErrH
Dim rsTemp As New ADODB.Recordset, sLastBackupDate As String, bBackupReqd As Boolean
With rsTemp
.Open "SELECT * FROM [_AppSettings] WHERE SettingName='DatabaseLastArchiveDate'", cnnDB, adOpenStatic, adLockReadOnly, adCmdText
If Not .EOF Then sLastBackupDate = .Fields("SettingValue")
.Close
End With
Set rsTemp = Nothing
If Not IsDate(sLastBackupDate) Then
bBackupReqd = True
ElseIf Abs(DateDiff("h", CDate(sLastBackupDate), Now)) >= 23 Then
bBackupReqd = True
Else
bBackupReqd = False
End If
If bBackupReqd Then
If vbYes = MsgBox("Atleast one day has passed since you last backed up your database." & vbCrLf & vbCrLf & "Do you wish to create a backup of your database now?", vbQuestion + vbYesNo, "Backup Database?") Then miBackupDB_Click
End If
bForceUnload = True
DoEvents
Call TerminateApp
Exit Sub
ErrH:
ErrorOut Err.Number, Err.Description
Call TerminateApp
End Sub
'****** In Module ******************
Sub TerminateApp()
''Terminates application and releases all used resources
On Error Resume Next
xlApp.Quit
wdApp.Quit
Set xlApp = Nothing
Set wdApp = Nothing
End
End Sub
Pradeep
-
Sep 23rd, 2005, 02:09 PM
#8
Re: Windows Unable to Terminate my VB Application... WHY ???
I see your TerminateApp sub is referencing Word and Excel. Do you perhaps have left over variable objects for each that are still existing? This would cause Word or Excel to remain open.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 23rd, 2005, 02:24 PM
#9
Re: Windows Unable to Terminate my VB Application... WHY ???
 Originally Posted by RobDog888
I see your TerminateApp sub is referencing Word and Excel. Do you perhaps have left over variable objects for each that are still existing? This would cause Word or Excel to remain open.
Because I frequently need word and excel instances in the program, I just launch them at application startup (hidden) and close them when it is closing. This is just because they take a lot of time to start. I open my docments/workbooks in them as and when needed and after doing the job (usually formatting & printing), I close them but not the word & excel instances.
Though what you are saying may be true, it's a rare chance.
But anyway my problem is completely different. I never face problems loggingoff/shuttingdown after I have quitted the application. The problem only occurs when this app is running.
The second thing is that should the case be what you suspect, windows should show the End Task box (which it normally does if an application refuses to terminate normally), but this never happens. It just cancels teh shutdown operation.
Pradeep
-
Sep 23rd, 2005, 02:42 PM
#10
Re: Windows Unable to Terminate my VB Application... WHY ???
The shutdown order of events is
MDI QueryUnload
Child QueryUnload
Child Unload
MDI Unload
Since the Child does not Unload when a shutdown occurs (UnloadMode = vbFormMDIForm and blnForceUnload are both False - cancelling the QueryUnload), the MDI Unload event does not fire, TerminateApp is never called.
Not sure why you need the blnForceUnload. Just check if Windows is shutting down.
If Not (UnloadMode = vbAppWindows Or UnloadMode = vbFormMDIForm) Then
You might want to add a check for UnloadMode = vbAppTaskManager as well.
Last edited by brucevde; Sep 23rd, 2005 at 02:45 PM.
-
Sep 23rd, 2005, 03:35 PM
#11
Re: Windows Unable to Terminate my VB Application... WHY ???
 Originally Posted by brucevde
The shutdown order of events is
MDI QueryUnload
Child QueryUnload
Child Unload
MDI Unload
Since the Child does not Unload when a shutdown occurs (UnloadMode = vbFormMDIForm and blnForceUnload are both False - cancelling the QueryUnload), the MDI Unload event does not fire, TerminateApp is never called.
Not exactly! Just see this sequence which might be right according to u.
MDI QueryUnload - No code
Child QueryUnload - Gets hidden, but not unloaded
Child Unload - No code
MDI Unload - Calls TerminateApp
Not sure why you need the blnForceUnload. Just check if Windows is shutting down.
It is serving a dual purpose. Whenever I want to REALLY unload the child form from code, I set this boolean and unload it. So that it is not hidden, it is actually unloaded.
You might want to add a check for UnloadMode = vbAppTaskManager as well.
Task manager is able to terminate my app instantly without a hitch.
If Not (UnloadMode = vbAppWindows Or UnloadMode = vbFormMDIForm) Then
I think you are right. But this is not sufficient to justify that it will cancel the shutdown operation of windows. Whenever an app misbehaves or takes a long time to unload by itself after windows tells it to, windows shows the End Task box, which I never get. It simply cancels the shutdown operation.
Second, eventually my TerminateApp procedure calls END statement which should force termination of my program. So I believe somehow this procedure is not being called when Windows tells it to shutdown but is being called when Task manager tells it to do so. Why???
Pradeep
-
Sep 29th, 2005, 12:54 PM
#12
Re: Windows Unable to Terminate my VB Application... WHY ???
Oh.. I havn't got thru this till now
-
Oct 14th, 2005, 03:39 AM
#13
Re: Windows Unable to Terminate my VB Application... WHY ???
-
Oct 14th, 2005, 03:59 AM
#14
Re: Windows Unable to Terminate my VB Application... WHY ???
 Originally Posted by Pradeep1210
Thanks a lot Martin.
I'll try that tommorrow in my office. (the app is for office internal use)
BTW
The whole application is too big to be posted here. So here is just the relevent parts of the application. Hope this would give more insight about the problem:
VB Code:
'****** MDI Children Forms ******************
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If Not (UnloadMode = vbFormMDIForm Or bForceUnload) Then
Cancel = True
Me.Hide
End If
End Sub
'****** MDI Parent Form ******************
Private Sub MDIForm_Unload(Cancel As Integer)
On Error GoTo ErrH
Dim rsTemp As New ADODB.Recordset, sLastBackupDate As String, bBackupReqd As Boolean
With rsTemp
.Open "SELECT * FROM [_AppSettings] WHERE SettingName='DatabaseLastArchiveDate'", cnnDB, adOpenStatic, adLockReadOnly, adCmdText
If Not .EOF Then sLastBackupDate = .Fields("SettingValue")
.Close
End With
Set rsTemp = Nothing
If Not IsDate(sLastBackupDate) Then
bBackupReqd = True
ElseIf Abs(DateDiff("h", CDate(sLastBackupDate), Now)) >= 23 Then
bBackupReqd = True
Else
bBackupReqd = False
End If
If bBackupReqd Then
If vbYes = MsgBox("Atleast one day has passed since you last backed up your database." & vbCrLf & vbCrLf & "Do you wish to create a backup of your database now?", vbQuestion + vbYesNo, "Backup Database?") Then miBackupDB_Click
End If
bForceUnload = True
[B]DoEvents[/B]
Call TerminateApp
Exit Sub
ErrH:
ErrorOut Err.Number, Err.Description
Call TerminateApp
End Sub
'****** In Module ******************
Sub TerminateApp()
''Terminates application and releases all used resources
On Error Resume Next
xlApp.Quit
wdApp.Quit
Set xlApp = Nothing
Set wdApp = Nothing
[B]End[/B]
End Sub
Pradeep
Have you tried removing the DoEvents and End in your code?
-
Oct 14th, 2005, 04:25 AM
#15
Fanatic Member
Re: Windows Unable to Terminate my VB Application... WHY ???
I think you should take a look at subclassing a window to catch the WM_QUERYENDSESSION Windows message and initiate a shutdown routine at that time. I've had apps in the past which caused Windows to refuse to shutdown or restart until I this was implemented at least. It seems most prevelant in apps which start in Sub Main rather than a form.
VBAccelerator has a SSubTimer6.dll which works great for that and makes subclassing a breeze. They even have a example of how to implemente this.
Also, if this app is running under XP you might want to look at adding handlers for the WM_WTSSESSION_CHANGE, WTS_CONSOLE_CONNECT, WTS_CONSOLE_DISCONNECT, etc, messages also (there's also logon and logoff messages as well as lock and unlock).
-
Oct 14th, 2005, 04:27 AM
#16
Re: Windows Unable to Terminate my VB Application... WHY ???
 Originally Posted by dee-u
Have you tried removing the DoEvents and End in your code?
I did. There was no difference.
Pradeep
-
Oct 14th, 2005, 12:39 PM
#17
Re: Windows Unable to Terminate my VB Application... WHY ???
Windows tries to do an orderly shutdown. Task Manager simply wipes out the process. The fix is simple and has already been posted. All you need to do is check if the UnloadMode is equal to vbAppWindows. The fact that you want/get a prompt to "End the Task Now" is irrelevant and out of your control.
The MDIForm_Unload event will not fire if a Child form is still active (hidden or not) then obviously your TerminateApp procedure is not called.
Again (based on the code you posted) the bForceUnload variable is pointless.
-
Oct 14th, 2005, 01:38 PM
#18
Re: Windows Unable to Terminate my VB Application... WHY ???
 Originally Posted by brucevde
Windows tries to do an orderly shutdown. Task Manager simply wipes out the process....
I've lost track of where this thread is going but I just wanted to point out that you can interrupt the Task Manager shut down.
VB Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Select Case UnloadMode
Case vbAppTaskManager
If vbNo = MsgBox("Do you want to quit?", vbYesNo) Then Cancel = True
End Select
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|