CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 29
  1. #1
    Join Date
    Apr 2003
    Posts
    322

    Unload Form does not always terminate program execution

    I've seen several cases where all of the forms in my app are closed, yet dialog boxes still pop up, indicating a problem.

    This makes the users feel uneasy :-)

    What is the proper way to COMPLETELY terminate execution in a VB app, after all the forms have been unloaded ?

  2. #2
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487
    Just make sure that after you have unloaded the form there shoud be no code in the program that makes use of the form.
    Busy

  3. #3
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    31
    I agree with Thread1. If there is a need for a form to call an object in another form, programmer must explicitly define a way so that the calling form first check if the owner of the object to be called is loaded or not.
    I am inviting you to join GreenZap. It is just like PAYPAL but it is more rewarding! You get $25 when you join (FREE to sign up) AND you get rewarded also when someone opened an account using your GreenZap promo code! GreenZap is launching June 1st.

    Click here and PREREGISTER

    Have a break. Visit JonelsPlace

  4. #4
    Join Date
    Dec 2001
    Posts
    6,332
    Oddly enough, I've seen cases where a simple single-form program won't completely unload on a system running winME, but on other OS's it's ok. I used End as the last line of code after the form unloads, which seems to have solved the problem.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Well...are you sure that all forms are unloaded...? You can check it within the 'Query_Unload' evetn of the main dialog...add the following
    Code:
    Dim FormX as Form
    For Each FormX in Forms
      if FormX.hWnd<> Me.hWnd then
        Debug.Print "Form is still loaded"
        Unload FormX
      End If
    Next

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

    Avoid End command

    Now, read these threads

    http://www.codeguru.com/forum/showth...ight=avoid+end

    http://www.codeguru.com/forum/showth...ight=avoid+end

    and avoid "End" (unless on a very bugged Os...)



    Ps:
    Andreas, nice snippet
    ...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.

  7. #7
    Join Date
    Dec 2001
    Posts
    6,332
    Cimperiali
    Yes, I know that using End isn't supposed to be good, but when you do a test like the following and the program stays in memory (seen in task manager), what then?
    Code:
    'absolutely no code whatsoever. just a blank form.
    'No objects of any kind. No controls either.
    A totally blank form compiled into an exe stays running on winME only.

    So, I tried "Unload Me" and of course that didn't work. Using End in the Form_Unload sub did the trick. Sure it just terminates the program, but if it's used AFTER everything has been handled the right way anyway, then I would think it wouldn't be so bad.

    Basically, I did it this way:
    Code:
    Private Sub Form_Unload(Cancel As Integer)
    'set all objects to nothing
    'unload any forms still loaded
    End
    End Sub
    Since the form is unloading at this point anyway, what's left but to terminate?

    If there's a better way to do it I'd sure like to know, as I didn't want to use End either.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  8. #8
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452
    I have alwasy been a major opponent of using the END statement. However, I will have to agree with WizBang on this one. The program I wrote for our company had nothing but problems on Windows ME. Out of memory errors, stack overflow, and just plain not ending properly. Things worked fine on all our 98/2000 systems. For us it was an easy decision to spend the money and upgrade to 2000. Unfortunately, this can't always be done. My suggestion is that in your VB apps, you state that will run on 98/200/XP. Leave ME out of it. But if you have to put your app on ME. please, by all means use the END.


  9. #9
    Join Date
    Nov 2002
    Posts
    82
    Why not get the form's address and free it?

    James
    james

  10. #10
    Join Date
    Apr 2003
    Posts
    322
    Quote Originally Posted by jamesa
    Why not get the form's address and free it?

    James
    And how is that done ?

  11. #11
    Join Date
    Apr 2003
    Posts
    322
    and avoid "End" (unless on a very bugged Os...)

    Since VB only runs on Windows, all of those OS's are very buggy.
    W98 is one of the worst of the lot.

  12. #12
    Join Date
    Apr 2003
    Posts
    322
    Thanks for all the great answers- it looks like End should not be used.

    One of The problems with the program I am maintaining is
    it has about 10 forms, and the person who wrote it iriginally, never checked for errors during executuion.
    This program triggers an oscilloscope through GPIB, and if the scope probe isn't connected securely to a test point, the scope wont trigger. But the code which calls the triggering code, calls it many times.

    When the scope doesn't trigger, the users typically exit the program, and restart it. When the user selects Exit from any of the Form Menus this code gets called (which is in a .BAS file)

    Sub ProgramEnd(CallingWindowHandle As Long)

    Dim Form As Form

    ' ProgramIsUnloading MUST be set here, or the forms wont be unloaded
    ProgramIsUnloading = True

    For Each Form In Forms
    If Form.hwnd <> CallingWindowHandle Then
    Unload Form
    End If
    Next

    end sub


    However, even though all of the forms are closed, the code trying to trigger the scope is still running, as verified by task manager.

    So when the user runs a second instance of the program ( the old instance is running, but not visible) both programs try to talk to the scope, which results in both programs hanging.
    I want to re-write the scope triggering code, so an error is returned, when the scope doens't trigger. However, that's a lot of code, so I was hoping for a quicker fix (like End, or something).

  13. #13
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    Did you stop timers and exit all loops?

    Btw, Wizbang: "unless on buggy Os" =that was exactly for Windows Me,
    a horrible one that I remember only for bugs...
    ...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.

  14. #14
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923
    ExitProcess seems to be a good way to end a program too, see this thread:

    http://www.codeguru.com/forum/showthread.php?t=206561

    I think everyone remember Windows ME as a really buggy os...

    JeffB
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  15. #15
    Join Date
    Jan 2002
    Location
    somewhere between a variable and a string
    Posts
    214

    Exclamation Triggering / de-triggering the code !

    It seems like the original coder failed to apply any " Exit Do " statement's
    within your Looping structure's

    Apply : " Exit Do " ' in your Loop's < This should basically be your pivot point ,
    Code:
     If Button = 1 then
                 Exit Do
                     End If
    Apply : " Do Events "' Between your Loop's < this will allow Windows to destroy
    ' Your program during the Unload Event

    The use of " End " in the Unload Event just forces Windows to crash exit the application , leading to an internally messy destroy .

    If there is any Blitting then more than likely the original coder failed to include the necessary " Destroy DC " , in which case Windows is still trying to write into memory .

    Really just guessing here , don't really know what the structure is.....
    How can anyone sell information when all the information that they sell is free at the library ?

    How come Doctors keep getting smarter yet they cannot cure the common cold ?

    If Scientist are right and man evolved from monkey then where did the monkeys' come from ?

    Man I love this little dude that waves => <= He just doesn't stop

Page 1 of 2 12 LastLast

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

Featured