Unload Form does not always terminate program execution - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 29 of 29

Thread: Unload Form does not always terminate program execution

  1. #16
    Join Date
    Dec 2001
    Posts
    6,330
    This is interesting. I made a small test program with the following code:
    Code:
    Option Explicit
    Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
    
    Private Sub Command1_Click()
    ExitProcess 0&
    End Sub
    
    Private Sub Command2_Click()
    End
    End Sub
    
    Private Sub Command3_Click()
    Unload Me
    End Sub
    I then watched with a API spy tool to see what was happening for each button. Basically, they all call ExitProcess, but calling ExitProcess in code seems to do it more rapidly. The others had a lot of GetCurrentThreadId calls and a few Sleep 0 calls. The "X" button of the form wasn't really different either. Oddly, using End also made a call to LoadLibrary for oleaut32 and then GetProcAddress for "SysAllocString". I know the tool I use doesn't reveal everything, so maybe someone else can add to this.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  2. #17
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    Very interesting.
    Now, what happens if you have a project that instantiate a dll, and before
    releasing you try the different kind of exit?
    ...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
    Apr 2003
    Posts
    322
    [QUOTE=Cimperiali]Did you stop timers and exit all loops?


    Not explicitly. The form that is sending the commands to the scope is still running.
    My understanding of unloading a form, is that all code executing in a form is out of scope, once the form is unloaded.

    Since my ProgramEnd() unloads all forms except the calling form, I expected that to take care of all running code.

    Bad assumption, I guess.

  4. #19
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    Not true: loop or timers could keep your form alive, preventing unloading
    (well, matter is what you do inside loops or timers event...)

    Better you exit the loops and stop the timers -expecially Api timers...
    Last edited by Cimperiali; August 5th, 2004 at 06:09 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.

  5. #20
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    For example (the thing is due to the doEvents...)
    unloading form that has the following code (timer interval 200 ms)
    will not stop program - in ide and in compiled (you will have to
    terminate it hitting the square button in ide[=taht is an "End"!]
    or using task manager if running a compiled...[that should be an
    ExitProcess...], even if you do not see the form.
    Code:
    Option Explicit
    
    Private Sub Timer1_Timer()
       Static Counter As Long
       Counter = Counter + 1000
       Call loopme(Counter)
     
    End Sub
    Private Sub loopme(ByVal Counter As Long)
     Dim lstop As Long
       Do While Counter > lstop
          DoEvents
          Me.Caption = "Hi!" & Counter
          lstop = lstop + 1
       Loop
    End Sub
    ...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.

  6. #21
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    Code:
    'stop it this way....
    
    Option Explicit
    
    Private bQuitting as Boolean
    
    Private Sub Timer1_Timer()
       Static Counter As Long
       If bQuitting Then
               Timer1.Enabled = false 
               Exit Sub
        End If 
    
       Counter = Counter + 1000
       Call loopme(Counter)
      
    End Sub
    Private Sub loopme(ByVal Counter As Long)
     Dim lstop As Long
       Do While Counter > lstop
          DoEvents 'after this,other  messages are processed...!
          If bQuitting Then 'So here bQuitting may be changed...here you can get it!
               Exit Do 
          End If 
          Me.Caption = "Hi!" & Counter
          lstop = lstop + 1
       Loop
    End Sub
    
    Private sub Form_queryUnload(...)
        Timer1.Enabled = False
        bQuitting= True 
    end sub
    Last edited by Cimperiali; August 6th, 2004 at 01:23 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.

  7. #22
    Join Date
    Dec 2001
    Posts
    6,330
    It just occured to me what might be the reason for the Sleep 0 calls I see when a vb program unloads. Could this be the API equivelent of DoEvents? Maybe later when I find the time I will do a few tests. However, the API spy tool I use is almost certainly leaving out some things. Any suggestions on good tools to use would certainly be helpful in sorting through all this.
    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. #23
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,922
    I don't think it's the equivalent of DoEvents, I think it's just a way to let the system execute other exe and threads and return as soon as possible to the program. I think DoEvents would look more like this:

    http://www.vbcodelibrary.co.uk/modul...c=1204&forum=3

    Anyway if you find anything I would like to know more too.

    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

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

    Re:api Spy !

    Click Here for : API SPY !


    Quote Originally Posted by WizBang
    It just occured to me what might be the reason for the Sleep 0 calls I see when a vb program unloads. Could this be the API equivelent of DoEvents? Maybe later when I find the time I will do a few tests. However, the API spy tool I use is almost certainly leaving out some things. Any suggestions on good tools to use would certainly be helpful in sorting through all this.
    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

  10. #25
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487
    Quote Originally Posted by cappy2112
    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).
    It seems to me that there is something wrong with the code.. What is that CallingWindowHandle suppossed to mean in the code? Could you post at least one of that Exit menu routines?
    Busy

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

    To Thread1

    Thread1, if you are unloading all forms from a Query_unload or Unload event of
    a form, it make no sense to trig the unload twice by cycling throug all forms:
    you can skip the one from which you triggered the unloadAll code, which is already
    unloading
    enche:
    CallingWindowHandle has inside the Hwnd of the calling -and already unloading-
    form. ie:
    Code:
    private sub form Query_Unload(...)
       call UnloadAll(Me.Hwnd) 
    end sub
    
    Public sub UnloadAll(byval CallingWindowHandle  as long)
       dim frm as form
       for each frm In Forms
           if frm.hwnd<>CallingWindowHandle   then
                unload frm 'all but the caller, which is already unloading - 
                           'else its unload event will trig again...
           end if
       next 
    end sub
    ...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.

  12. #27
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487
    Crystal clear Cimp thanks for that .. Apology.. I just thought there is a User menu, named Exit, in the form and the Sub is being called there..
    Busy

  13. #28
    Join Date
    Apr 2003
    Posts
    322
    Quote Originally Posted by Thread1
    It seems to me that there is something wrong with the code.. What is that CallingWindowHandle suppossed to mean in the code? Could you post at least one of that Exit menu routines?
    Cimperiali is correct.

    The code I posted gets called from QueryUnload, in every form in the project.
    The CallingWindowHandle is the handle of the form which calls the code to unload all of the forms. I don't want the Unload event for the calling form, to get triggered again.

  14. #29
    Join Date
    Apr 2003
    Posts
    322
    Quote Originally Posted by Cimperiali
    Not true: loop or timers could keep your form alive, preventing unloading
    (well, matter is what you do inside loops or timers event...)

    Better you exit the loops and stop the timers -expecially Api timers...
    I agree with you.
    I've just looked at the project again- and the code which triggers the scope is called from at least 10 different places, which means I would have to check for the unload event (via a global flag) from many, many places.

    I tried a fix, but ended up with lots of variables not defined ina With block problems.

    At this point, it would just make the code more messy than it already is.
    I don't want to hack into that now. I'll have to think about another way to keep the app from restarting, once all of the forms are closed.

    Hmm- how about this, if queryunload is called, I can set a ProgramIsUnloading flag, and check to see if it is set in FormActivate, or FormLoad. THis should keep the forms from reappearing, but wouldn't stop the code that is talking to the scope.

    What do you think ?

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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center