Results 1 to 18 of 18

Thread: Windows Unable to Terminate my VB Application... WHY ???

  1. #1

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Arrow 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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  2. #2
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    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.

  3. #3

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Windows Unable to Terminate my VB App.

    Quote 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
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Windows Unable to Terminate my VB Application

    Any Ideas ??
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Windows Unable to Terminate my VB Application

    Anyone ??
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Windows Unable to Terminate my VB Application... WHY ???

    This is a guess but have you tried something like this?

    VB Code:
    1. Private Sub MDIForm_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    2.  
    3.     Select Case UnloadMode
    4.         Case vbAppWindows, vbAppTaskManager
    5.             ' Set your boolean and then...?
    6.     End Select
    7.  
    8. End Sub

    BTW, bumping threads is frowned on, especially only afrer an hour or so.

  7. #7

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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:
    1. '****** MDI Children Forms ******************
    2. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    3.     If Not (UnloadMode = vbFormMDIForm Or bForceUnload) Then
    4.         Cancel = True
    5.         Me.Hide
    6.     End If
    7. End Sub
    8.  
    9. '****** MDI Parent Form ******************
    10. Private Sub MDIForm_Unload(Cancel As Integer)
    11.     On Error GoTo ErrH
    12.     Dim rsTemp As New ADODB.Recordset, sLastBackupDate As String, bBackupReqd As Boolean
    13.     With rsTemp
    14.         .Open "SELECT * FROM [_AppSettings] WHERE SettingName='DatabaseLastArchiveDate'", cnnDB, adOpenStatic, adLockReadOnly, adCmdText
    15.         If Not .EOF Then sLastBackupDate = .Fields("SettingValue")
    16.         .Close
    17.     End With
    18.     Set rsTemp = Nothing
    19.     If Not IsDate(sLastBackupDate) Then
    20.         bBackupReqd = True
    21.     ElseIf Abs(DateDiff("h", CDate(sLastBackupDate), Now)) >= 23 Then
    22.         bBackupReqd = True
    23.     Else
    24.         bBackupReqd = False
    25.     End If
    26.     If bBackupReqd Then
    27.         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
    28.     End If
    29.     bForceUnload = True
    30.     DoEvents
    31.     Call TerminateApp
    32.     Exit Sub
    33. ErrH:
    34.     ErrorOut Err.Number, Err.Description
    35.     Call TerminateApp
    36. End Sub
    37.  
    38. '****** In Module ******************
    39. Sub TerminateApp()
    40.     ''Terminates application and releases all used resources
    41.     On Error Resume Next
    42.     xlApp.Quit
    43.     wdApp.Quit
    44.     Set xlApp = Nothing
    45.     Set wdApp = Nothing
    46.     End
    47. End Sub

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  9. #9

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Windows Unable to Terminate my VB Application... WHY ???

    Quote 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
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  10. #10
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    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.

  11. #11

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Windows Unable to Terminate my VB Application... WHY ???

    Quote 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
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  12. #12

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Windows Unable to Terminate my VB Application... WHY ???

    Oh.. I havn't got thru this till now
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  13. #13

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Windows Unable to Terminate my VB Application... WHY ???

    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  14. #14
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Windows Unable to Terminate my VB Application... WHY ???

    Quote 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:
    1. '****** MDI Children Forms ******************
    2. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    3.     If Not (UnloadMode = vbFormMDIForm Or bForceUnload) Then
    4.         Cancel = True
    5.         Me.Hide
    6.     End If
    7. End Sub
    8.  
    9. '****** MDI Parent Form ******************
    10. Private Sub MDIForm_Unload(Cancel As Integer)
    11.     On Error GoTo ErrH
    12.     Dim rsTemp As New ADODB.Recordset, sLastBackupDate As String, bBackupReqd As Boolean
    13.     With rsTemp
    14.         .Open "SELECT * FROM [_AppSettings] WHERE SettingName='DatabaseLastArchiveDate'", cnnDB, adOpenStatic, adLockReadOnly, adCmdText
    15.         If Not .EOF Then sLastBackupDate = .Fields("SettingValue")
    16.         .Close
    17.     End With
    18.     Set rsTemp = Nothing
    19.     If Not IsDate(sLastBackupDate) Then
    20.         bBackupReqd = True
    21.     ElseIf Abs(DateDiff("h", CDate(sLastBackupDate), Now)) >= 23 Then
    22.         bBackupReqd = True
    23.     Else
    24.         bBackupReqd = False
    25.     End If
    26.     If bBackupReqd Then
    27.         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
    28.     End If
    29.     bForceUnload = True
    30.     [B]DoEvents[/B]
    31.     Call TerminateApp
    32.     Exit Sub
    33. ErrH:
    34.     ErrorOut Err.Number, Err.Description
    35.     Call TerminateApp
    36. End Sub
    37.  
    38. '****** In Module ******************
    39. Sub TerminateApp()
    40.     ''Terminates application and releases all used resources
    41.     On Error Resume Next
    42.     xlApp.Quit
    43.     wdApp.Quit
    44.     Set xlApp = Nothing
    45.     Set wdApp = Nothing
    46.     [B]End[/B]
    47. End Sub

    Pradeep
    Have you tried removing the DoEvents and End in your code?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  15. #15
    Fanatic Member
    Join Date
    Jan 2005
    Location
    In front of this pc.
    Posts
    580

    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).

  16. #16

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Windows Unable to Terminate my VB Application... WHY ???

    Quote Originally Posted by dee-u
    Have you tried removing the DoEvents and End in your code?
    I did. There was no difference.

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  17. #17
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    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.

  18. #18
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Windows Unable to Terminate my VB Application... WHY ???

    Quote 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:
    1. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    2.  
    3.     Select Case UnloadMode
    4.         Case vbAppTaskManager
    5.             If vbNo = MsgBox("Do you want to quit?", vbYesNo) Then Cancel = True
    6.     End Select
    7.  
    8. 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
  •  



Click Here to Expand Forum to Full Width