DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Unable to Unload and/or End Program Properly

  1. #1
    Join Date
    Apr 2007
    Location
    Tennessee, USA
    Posts
    6

    Unable to Unload and/or End Program Properly

    I have tried as many ways as I can think to end this program in a proper manner. I have even created an UnloadAllForms function as you will see below, and that still is not allowing me the proper exiting of the application.

    First let me start by defining "Exit Properly". This means that when you no longer see the form it is no longer in the Task Manager. For some reason I am unable to make this program do that as it should. I have tried for the last two hours to make this work.

    I know using End isn't recommended by Microsoft, and I would much prefer I didn't have to use it as they recommend.

    Here's the code as I have it sitting now.

    File > Exit
    Code:
    Private Sub mnuFileExit_Click()
        Form_Unload (False)
    End Sub
    Form_Unload
    Code:
    Private Sub Form_Unload(Cancel As Integer)
        Wsk.Close
        WskUsers.Close
        DXTimer = False
        AbandonShip
        UnloadAllForms
    End Sub
    AbandonShip
    Code:
    Public Sub AbandonShip()
        Dim X As Long
        
        WriteVar strDAT & "Game.Dat", "LASTSETTINGS", "Map", MapID
    
        'Store the error log
        'Open App.path & "\GameErrorLog.txt" For Output As #1
        'Print #1, frmDebug.txtLog.Text
        'Close #1
        
        For X = 0 To UBound(ArrButtons)
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "Name", ArrButtons(X).Name
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "Tip", ArrButtons(X).Tip
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "Command", ArrButtons(X).Command
        Next X
        
        For X = 37 To 40
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "Name", ArrTourns(X).Name
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "Cost", ArrTourns(X).Cost
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "Players", ArrTourns(X).Players
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "PPT", ArrTourns(X).PPT
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "Rules", ArrTourns(X).Rules
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "Style", ArrTourns(X).Style
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "Teams", ArrTourns(X).Teams
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "Type", ArrTourns(X).Type
        Next X
    
        WriteVar strDAT & "Game.Dat", "Options", "KeyPad", frmMain.mnuOptionsKeypad.Checked
        
        WriteVar strDAT & "Game.Dat", "Options", "Sound", bSound
        WriteVar strDAT & "Game.Dat", "Options", "Music", bMusic
        WriteVar strDAT & "Game.Dat", "Options", "RandomSongs", bRandomSongs
        
        'Various options**********************************************************
        WriteVar strDAT & "Game.Dat", "Options", "Tags", frmMain.mnuOptionsTags.Checked
        WriteVar strDAT & "Game.Dat", "Options", "LangFilter", frmMain.mnuOptionLangFilter.Checked
        WriteVar strDAT & "Game.Dat", "Options", "AutoChat", frmMain.mnuOptionsChat.Checked
        WriteVar strDAT & "Game.Dat", "Options", "AutoStore", frmMain.mnuOptionAutoStore.Checked
        WriteVar strDAT & "Game.Dat", "Options", "AutoAdmin", frmMain.mnuOptionsAdmin.Checked
        WriteVar strDAT & "Game.Dat", "Options", "IgnoreLogins", frmMain.mnuOptionLoginIgnore.Checked
        WriteVar strDAT & "Game.Dat", "Options", "IgnoreWhoSplats", frmMain.mnuOptionSplatIgnore.Checked
        WriteVar strDAT & "Game.Dat", "Client Data", "Time", Client.Time
        WriteVar strDAT & "Game.Dat", "Client Data", "Season", Client.Season
        WriteVar strDAT & "Game.Dat", "Options", "Splitter", frmMain.Splitter.SplitterTop
        
        'FONT TYPE**************************************************************
        WriteVar strDAT & "Game.Dat", "Settings", "FontType", TextFont.StandardName
        WriteVar strDAT & "Game.Dat", "Settings", "FontType2", TextFont.SpecialName
        WriteVar strDAT & "Game.Dat", "Settings", "FontSize", CStr(TextFont.Size)
        WriteVar strDAT & "Game.Dat", "Settings", "BoldNames", CStr(TextFont.BoldNames)
        WriteVar strDAT & "Game.Dat", "Settings", "Italic", CStr(TextFont.Italic)
        
        'TEXT COLOURS***********************************************************
        WriteVar strDAT & "Game.Dat", "Colours", "Chat", ColChat.ColName
        WriteVar strDAT & "Game.Dat", "Colours", "Tell", ColTell.ColName
        WriteVar strDAT & "Game.Dat", "Colours", "Plan", ColPlan.ColName
        WriteVar strDAT & "Game.Dat", "Colours", "Announce", ColAnno.ColName
        
        DestroyDX
    End Sub
    UnloadAllForms
    Code:
    Public Sub UnloadAllForms()
        Dim objForm As Form
        
        For Each objForm In Forms
            Unload objForm
        Next
    End Sub
    That in my personal opinion should do the trick. It should always properly save my settings, and exit my application. WRONG. It doesn't work at all for me. If I add an End in my UnloadAllForms function the File > Exit "Exits Properly", yup you guessed it, exiting any other way doesn't work!

    If anyone can save the last bit of hair that I have left I would greatly appreciate it! I have run up against it on this one. I am brain dead at this point from trying this problem so hard!

    Thanks in advance,
    JS

  2. #2
    Join Date
    Aug 2005
    Posts
    85
    I would try it like this;

    Code:
    Private Sub mnuFileExit_Click()
        Unload Me
    End Sub
    Code:
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        Dim Frm As Form    
        Wsk.Close
        WskUsers.Close
        DXTimer = False
        AbandonShip
        On Error Resume Next
        For Each Frm In Forms
            Unload Frm
        Next
    End Sub
    Let us know if this works.

  3. #3
    Join Date
    Apr 2007
    Location
    Tennessee, USA
    Posts
    6
    I just tried that, and it unfortunately didn't work!?

    I have tried many things similar, and they didn't work either. Any more ideas? I am up for just about anything except: disabling the "X" button, and then having to call "End".

    Thanks for everything so far!
    JS

  4. #4
    Join Date
    Aug 2005
    Posts
    85
    Well JS, this is a tricky one!

    I wonder if it is to do with all the calls to frmMain in Abandonship.

    So, although we don't like to use 'End' because it causes immediate stopping of a programme with no chance to clean-up, perhaps you can try this;

    Code:
    Private Sub Form_Terminate()
        If (Forms.Count = 0) Then 'If all forms are gone then there should be no more cleaning up to do!
            End
        End If
    End Sub
    BTW, you did move your code from Unload to QueryUnload didn't you?

    Steve.

  5. #5
    Join Date
    Apr 2007
    Location
    Tennessee, USA
    Posts
    6
    Yes I moved my code. I removed Form_Unload completely, then after your suggestion I did the following.
    Code:
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        Dim objForm As Form
        Wsk.Close
        WskUsers.Close
        DXTimer = False
        AbandonShip
        UnloadAllForms
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        If (Forms.count > 0) Then UnloadAllForms
        If (Forms.count = 0) Then End
    End Sub
    
    Private Sub Form_Terminate()
        If (Forms.count > 0) Then UnloadAllForms
        If (Forms.count = 0) Then End
    End Sub
    Still not working! This is quite odd. I then tried the following, as I tested, and Form_Terminate is never called no matter how I exit the program. Unless maybe it's called from the Task Manager Exit.
    Code:
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        Dim objForm As Form
        Wsk.Close
        WskUsers.Close
        DXTimer = False
        LightningTimer = False
        tmrDis = False
        tmrUpdate = False
        AbandonShip
        UnloadAllForms
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        If (Forms.count > 0) Then UnloadAllForms
        If (Forms.count = 0) Then End
    End Sub
    This is still not working either. =/ I dont know what is so hard about getting this to work.

    Thanks for your help, any more ideas would be greatly appreciated!
    JS

  6. #6
    Join Date
    Aug 2005
    Posts
    85
    Well, short of seeing your entire code, I can only think that there is still a reference to one or more of your forms in a module somewhere.

    Let's be clear; If frmMain is your last loaded form and I mean last, no forms still loaded but invisible, then unloading the form will terminate your programme. We have tried to make sure that all forms are unloaded, so there must be a reference to a form somewhere in a bas module or a class.

    In the MSDN help it states for the Terminate Event;

    Occurs when all references to an instance of a Form, MDIForm, User control, Property Page, Webclass, DHTML Page Designer, or class are removed from memory by setting all the variables that refer to the object to Nothing or when the last reference to the object falls out of scope.
    Try and see if you can step through the shutdown code and see if something is causing a form to become loaded again, even if it remains invisible.

    Steve.

  7. #7
    Join Date
    Apr 2007
    Location
    Tennessee, USA
    Posts
    6
    I have figured out that even after closing ALL of the open forms several times there are always still two left open for some reason. frmMain, and frmTournament. I am going to look into frmTournament for any variables that need to be finished. I am assuming these are the last two open forms based on the following sections of code.

    frmMain
    Code:
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        On Error Resume Next
        Wsk.Close
        WskUsers.Close
        DXTimer = False
        LightningTimer = False
        tmrDis = False
        tmrUpdate = False
        AbandonShip
        UnloadAllForms
    End Sub
    
    Private Sub Form_Terminate()
        On Error Resume Next
        UnloadAllExcept Me.Name
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        On Error Resume Next
        If (Forms.count > 0) Then
            MsgBox "Count: " & str$(Forms.count)
            UnloadAllExcept Me.Name
        End If
        
        MsgBox "Final Count: " & str$(Forms.count)
        'If (Forms.Count = 0) Then End
    End Sub
    modJSFuncs
    Code:
    Public Sub UnloadAllForms()
        On Error Resume Next
        Dim objForm As Form
        
        For Each objForm In Forms
            MsgBox "Form: " & objForm.Name & " Count: " & str$(Forms.count)
            Unload objForm
            Set objForm = Nothing
        Next
    End Sub
    
    Public Sub UnloadAllExcept(frmName As String)
        On Error Resume Next
        Dim objForm As Form
        
        For Each objForm In Forms
            If objForm.Name <> frmName Then
                MsgBox "Form: " & objForm.Name & " Count: " & str$(Forms.count)
                Unload objForm
                Set objForm = Nothing
            End If
        Next
    End Sub
    I dont know what else to do. I am going to see if I can figure out whats up in the frmTournament.

    Thanks,
    JS

  8. #8
    Join Date
    Apr 2007
    Location
    Tennessee, USA
    Posts
    6
    I just found out that the frmTournament is being hidden in its Form_Unload.

    The reason being so that the information in the form stays the same. Should I explicitly unload this form from my main form? I need it to hide and unhide. I just put Unload Me to be sure it was that form causing all of the trouble, and it is.

    Thanks,
    JS

  9. #9
    Join Date
    Apr 2007
    Location
    Tennessee, USA
    Posts
    6
    I just decided to Unload Me on that form so that it goes away. I am not sure why my UnloadAllForms() function didn't do the trick, but I am very happy to have this bug fixed! This has been something plaguing the different owners of this software through a couple of programmers and years.

    I didn't actually try your code due to having a similar code already in my modJSFunc module.

    Below is the exact code I am using to close my application up.

    Form_QueryUnload
    Code:
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        On Error Resume Next
        AbandonShip
    End Sub
    AbandonShip
    Code:
    Public Sub AbandonShip()
        Dim X As Long
        
        frmMain.LightningTimer = False
        frmMain.DXTimer = False
        frmMain.tmrUpdate = False
        frmMain.Wsk.Close
        frmMain.WskUsers.Close
        
        WriteVar strDAT & "Game.Dat", "LASTSETTINGS", "Map", MapID
    
        'Store the error log
        Open App.path & "\GameErrorLog.txt" For Output As #1
        Print #1, frmDebug.txtLog.Text
        Close #1
        
        For X = 0 To UBound(ArrButtons)
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "Name", ArrButtons(X).Name
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "Tip", ArrButtons(X).Tip
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "Command", ArrButtons(X).Command
        Next X
        
        For X = 37 To 40
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "Name", ArrTourns(X).Name
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "Cost", ArrTourns(X).Cost
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "Players", ArrTourns(X).Players
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "PPT", ArrTourns(X).PPT
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "Rules", ArrTourns(X).Rules
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "Style", ArrTourns(X).Style
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "Teams", ArrTourns(X).Teams
            WriteVar strDAT & "Buttons.Dat", "Button" & X, "Type", ArrTourns(X).Type
        Next X
    
        WriteVar strDAT & "Game.Dat", "Options", "KeyPad", frmMain.mnuOptionsKeypad.Checked
        
        WriteVar strDAT & "Game.Dat", "Options", "Sound", bSound
        WriteVar strDAT & "Game.Dat", "Options", "Music", bMusic
        WriteVar strDAT & "Game.Dat", "Options", "RandomSongs", bRandomSongs
        
        'Various options**********************************************************
        WriteVar strDAT & "Game.Dat", "Options", "Tags", frmMain.mnuOptionsTags.Checked
        WriteVar strDAT & "Game.Dat", "Options", "LangFilter", frmMain.mnuOptionLangFilter.Checked
        WriteVar strDAT & "Game.Dat", "Options", "AutoChat", frmMain.mnuOptionsChat.Checked
        WriteVar strDAT & "Game.Dat", "Options", "AutoStore", frmMain.mnuOptionAutoStore.Checked
        WriteVar strDAT & "Game.Dat", "Options", "AutoAdmin", frmMain.mnuOptionsAdmin.Checked
        WriteVar strDAT & "Game.Dat", "Options", "IgnoreLogins", frmMain.mnuOptionLoginIgnore.Checked
        WriteVar strDAT & "Game.Dat", "Options", "IgnoreWhoSplats", frmMain.mnuOptionSplatIgnore.Checked
        WriteVar strDAT & "Game.Dat", "Client Data", "Time", Client.Time
        WriteVar strDAT & "Game.Dat", "Client Data", "Season", Client.Season
        WriteVar strDAT & "Game.Dat", "Options", "Splitter", frmMain.Splitter.SplitterTop
        
        'FONT TYPE**************************************************************
        WriteVar strDAT & "Game.Dat", "Settings", "FontType", TextFont.StandardName
        WriteVar strDAT & "Game.Dat", "Settings", "FontType2", TextFont.SpecialName
        WriteVar strDAT & "Game.Dat", "Settings", "FontSize", CStr(TextFont.Size)
        WriteVar strDAT & "Game.Dat", "Settings", "BoldNames", CStr(TextFont.BoldNames)
        WriteVar strDAT & "Game.Dat", "Settings", "Italic", CStr(TextFont.Italic)
        
        'TEXT COLOURS***********************************************************
        WriteVar strDAT & "Game.Dat", "Colours", "Chat", ColChat.ColName
        WriteVar strDAT & "Game.Dat", "Colours", "Tell", ColTell.ColName
        WriteVar strDAT & "Game.Dat", "Colours", "Plan", ColPlan.ColName
        WriteVar strDAT & "Game.Dat", "Colours", "Announce", ColAnno.ColName
        DestroyDX
    End Sub
    Form_Unload
    Code:
    Private Sub Form_Unload(Cancel As Integer)
        If (Forms.count > 0) Then UnloadAllExcept Me.Name
    End Sub
    Thanks to everyone for their help! I have the situation resolved.

    JS

Similar Threads

  1. Replies: 10
    Last Post: 07-09-2006, 01:10 AM
  2. WebCrawler program not working properly
    By bdjos1 in forum Java
    Replies: 0
    Last Post: 07-05-2006, 10:31 AM
  3. JMS: simple program (urgent please)
    By mhkateb in forum Java
    Replies: 1
    Last Post: 03-07-2005, 03:34 PM
  4. processing additional data when program is running
    By Ervin Rodriguez in forum Database
    Replies: 1
    Last Post: 05-16-2003, 08:45 AM
  5. Replies: 0
    Last Post: 12-15-2000, 11:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center