dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 30 of 30

Thread: program not fully closing

  1. #16
    Join Date
    May 2003
    Location
    Australia
    Posts
    155
    Hi again,

    After reading the last few replies, here are a few more things you could try ...

    1) If your Main() sub is only loading up the starting form, why not skip the Main() sub all together and set your form as the startup object? The reason I suggest this, is that you are creating a reference to your form in the Main() sub and loading via the reference, not via the form itself ... maybe this reference is not being cleared (set to Nothing), and as a reference still points to the form, it wont unload it. Or, alternatively, just load the form directly in your Main() sub rather than using a reference to it:

    Code:
    Sub Main()
    
      Load frmMain
      frmMain.Show
    
    End Sub
    This eliminates the chance that the reference to the form is not being cleared properly.

    2) Continues on from this idea ... make sure you dont have any other references pointing to a form. If any of your variables still points to a form, regardless of whether you unload it or not anywhere else, it will not be unloaded until all references to that form have been cleared (ie, set to Nothing).

    Based on the samples of code that you have been trying ... it should be working. There must be a variable reference somewhere in your code that is pointing to one of your forms that isnt getting cleared. It is the only thing I can think of that would result in "unloading all the forms in your forms collection" to not work.

    Try these ideas. If it still doesnt work, maybe (unless you object to anyone seeing your code) you would like to post it up, or email it to me or others so we can take a closer look?

    Cheers.
    Zen-Programming:

    If a compiler beeps in the IDE forest, and nobody hears it, was there really a bug?

  2. #17
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    a mistake?...

    and in every form's load event:

    If ClosingNow = True Then Unload Me: Exit Sub
    Thus, do you expect your code for unloading forms running while forms are loading?
    But your code running should close them too.
    Thus you're trying to unload a form in a routine while form maybe already unloading itself....
    Change code this way:
    1) remove the unloading in the load event of forms
    2)
    Code:
    Public Sub CloseProgram(ByVal frmPointer As Long)
            
        Dim Frm As Form
        Dim intCounter as integer
        Dim MaxForms as integer
    
         
        If Not ClosingNow Then
            ClosingNow = True
            MaxForms =Forms.count
            Do while froms.count > 1
                 intCounter = intCounter+1        
            'For Each Frm In Forms
                Debug.Print Frm.Name
                Debug.Print ObjPtr(Frm)
         
                'do not unload the caller
                If ObjPtr(Frm) <> frmPointer Then
                    Unload Frm
                End If
            'Next
                 if intCounter> MaxForms then
                       dim retVar as VbMsgBoxResult
                       retVar =msgbox("Already tried to unload some forms which do not unload. Try again?",vbyesno)
                       if retvar=vbNo then
                            exit do
                       end if 
                 end if
            Loop
          Set Frm = Nothing
        End If
         
    End Sub
    Last edited by Cimperiali; June 20th, 2003 at 06:29 AM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #18
    Join Date
    May 2002
    Location
    Huntington, WV
    Posts
    303
    i tried the "If ClosingNow = True Then Unload Me: Exit Sub" line cause of the suggestion from Tinbum747 that if the form wasn't unloaded when i went to unload it that it would be loaded for me. i do know just calling a form's name will call that form's load event so it sounded liek a good idea.

    i'll try the new code Cimp.

  4. #19
    Join Date
    May 2002
    Location
    Huntington, WV
    Posts
    303
    there's a problem with that code. i get an "object variable or with block variable not set" at the debug lines, then at the "If ObjPtr(Frm)" line when commenting those out.

    maybe the Frm varibale isn't being set to any particlar form. in which case it wouldn't have a .Name as in the line "Debug.Print Frm.Name". i dunno, cause this is a bit confusing to me, since i taught myself VB and so i don't know more advanced stuff like this.

    oh and there was a mistype, "Do while froms.count > 1", but i fixed it.

  5. #20
    Join Date
    May 2002
    Location
    Huntington, WV
    Posts
    303
    maybe i'll just write another program that i can call when my main program is unloaded. the 2nd program will check if the main app closed correctly, and if not, forcibly close it. then it will (hopefully...) close itself.

  6. #21
    Join Date
    Apr 2002
    Location
    Belgium
    Posts
    125
    isnt this working:

    Code:
    End
    Bert Willekens,

  7. #22
    Join Date
    May 2002
    Location
    Huntington, WV
    Posts
    303
    no....

  8. #23
    Join Date
    Feb 2003
    Location
    Greece
    Posts
    533

    dramatically solution

    Give us the full code / project in zip format.

    VB, the whole forum and the best codegurus of the world will promise for a solution to your problem.

    It appears that you have something wrong in your code, even if it seems to work on screen, memory conflict, object names mistypes or i cant imagine what else.

    Give us the full project!

    Thanks.

    (...or a brain short circuit will come...)

    The best "joke" i heard so far is that the END statement does not unload the program from task manager. This is completely crazy cause END is similar to TerminateProcess (as i know) and just kills the program even without freeing up memory and other resources.

    Anyway.... give us the....... program :-))))
    - Better live in the digital world -

  9. #24
    Join Date
    May 2002
    Location
    Huntington, WV
    Posts
    303
    i'm not posting my full source code... it's a commercial program, and this is just one small bug.


    anyways, i was gonna post another reply i already have typed up:
    oh my god.... i wrote a small app to forcibly close my program and it won't do it. i use this code all the time, but in this case it's saying the window doesn't exist. it finds every other program that's running, all kinds of stuff in the background, but not my program.



    Code:
    Public Function EndAllInstances(ByVal WindowCaption As String) As Boolean
        'INPUT: ANY PART OF THE WINDOW CAPTION
        'RETURNS: TRUE IF SUCCESSFUL (AT LEASE ONE PROCESS WAS STOPPED,
        'FALSE OTHERWISE)
        'EXAMPLE EndAllInstances "Notepad"
        Dim hwnd As Long
        Dim hInst As Long
        Dim hProcess As Long
        Dim lProcessID
        Dim bAns As Boolean
        Dim lExitCode As Long
        Dim lRet As Long
        
        On Error GoTo ErrorHandler
        
        If Trim(WindowCaption) = "" Then Exit Function
        Do
            hwnd = FindWin(WindowCaption)
            If hwnd = 0 Then Exit Do
            hInst = GetWindowThreadProcessId(hwnd, lProcessID)
            'Get handle to process
            hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0&, lProcessID)
            If hProcess <> 0 Then
                'get exit code
                GetExitCodeProcess hProcess, lExitCode
                If lExitCode <> 0 Then
                    'bye-bye
                    lRet = TerminateProcess(hProcess, lExitCode)
                    If bAns = False Then bAns = lRet > 0
                End If
            End If
        Loop
        
        EndAllInstances = bAns
    ErrorHandler:
    End Function
    Public Function FindWin(WinTitle As String) As Long
        
        Dim lhWnd As Long, sAns As String
        Dim sTitle As String
        lhWnd = Me.hwnd
        sTitle = LCase(WinTitle)
        Do
            DoEvents
            If lhWnd = 0 Then Exit Do
            sAns = LCase$(GetCaption(lhWnd))
                     
            If InStr(sAns, sTitle) Then
                FindWin = lhWnd
                Exit Do
            Else
                FindWin = 0
            End If
        
            lhWnd = GetNextWindow(lhWnd, 2)
        Loop
    End Function
    
    Private Function GetCaption(lhWnd As Long) As String
        Dim sAns As String, lLen As Long
    
        lLen& = GetWindowTextLength(lhWnd)
        sAns = String(lLen, 0)
        Call GetWindowText(lhWnd, sAns, lLen + 1)
        GetCaption = sAns
    End Function



    if that doesn't find it then why does Windows insist on even keeping the file in use and leaving it in the Task Manager list?.... if that code doesn't find it it means it doesn't have any handle at all, so there's no reason it should stay running... is there any way to just close a file, so i can just say "CloseFile app.path & "HTML Editor.exe"? cause that would be really great...

  10. #25
    Join Date
    May 2002
    Location
    Huntington, WV
    Posts
    303
    btw, cimperelli, how'd you get it to color-code the code? you ddin't have to keep entering in ibcode did ya?

  11. #26
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452
    Not exactly true. If your "Second" program can't find the window then the window you are looking for is closed. That doesn't mean that the program has ended. I would look in your sub Main for the problem. And if this is a commercial program, you need to really start looking at your coding. No one wants a program on there system that is going to eat up memory, not close or runs 2 parts just to work right. So post your code and let us fix it already.

    As for the color coding, check his sig.

  12. #27
    Join Date
    May 2002
    Location
    Huntington, WV
    Posts
    303
    i got to looking and there is a part of my program that closes it and it works every time. it's for when you cange an option and it has to be restarted to take effect:

    Code:
    Private Sub mnuToolsOptions_Click()
        On Error Resume Next
        requiresrestart = False
        frmOptions.Show vbModal, Me
        If requiresrestart = True Then
            Form_Unload 0
            End
        End If
        If ClrOnClose = True Then mnuViewRefreshColors_Click
        ClrOnClose = False
    End Sub

    tho for some reason if i change an option to one thing it always crashes my program and asks if i want to send the info to Microsoft (not just a VB runtime error). i dunno why setting the option tone thing does that and the other thing doesn't tho, each one just saves the setting and sets the requiresrestart variable.

    but anyways, why is it that code above works, when all it's doing is calling the unload sub.

  13. #28
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452
    You are getting the error because you use the End Statement and there is something in your program that is still trying to run. However, when you use the End Statement, all memory references are immediately terminated. So anything else (like windows trying to send your program messages) will generate this error. It all goes back to .....

    A. Unload all references to objects in your program.
    B. After that, Unload all the forms.
    C. Program exits normally

  14. #29
    Join Date
    May 2002
    Location
    Huntington, WV
    Posts
    303
    alright, i got it all working now. but whatcha mean by A?

  15. #30
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Unload all references to objects...

    ie:
    Close a recordset and set variable referencing it to nothing
    Code:
    dim rs as recordset
    set rs = new recordset
    '...
    rs.Close
    set rs= nothing
    Or:
    Close a connection and set variable referencing it to nothing
    Or:
    Close Word and set variable referencing it to nothing

    About colorcode, follow links in my signature.
    You will find sources.
    Last edited by Cimperiali; June 24th, 2003 at 12:44 PM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)