Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Pro VB 6
|
Pro VB 6 For advanced Visual Basic coders working in version 6 (not .NET). Beginning-level questions will be redirected to other forums, including Beginning VB 6.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 15th, 2006, 03:43 AM
Registered User
 
Join Date: Sep 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to orfano
Default VB application crashes when closing it using X

Lads,

I have a small VB6 application that does the following:

1- Using a button (connect) it starts a component which is a emulator for 3270 host.

then if I click in a second button (disconnect), it terminates this component calling a terminateObject function and the application stays in stand by.

In this moment if I close the application (using the X on the right upper corner) then in the Form_unload() funciton i call the terminateObject function again (just for been safe) and it closes normally.

However if I connect using the "first" button (connect) and then after I close the application using the X button (without using the desconnect button first) it does happen that the application closes and then i got a message error saying that the TstExtraObject.exe application has incountered a problem and has to be closed.

I noticed that if i put a MSgbox"hello" as last thing in the the Form_unload function then i don't get the error msg. I tried to use Sleep 10000 but it did not work.


So anyone has any idea how I could work around the issue. I will paste here part of my code (Thanks a million):



Function TerminateExtraObject(EventiExtra As ExtraEvents, FrameForExtra As Frame) As Boolean
    '__ATTENZIONE all'ordine con cui si terminano gli oggetti___

    EventiExtra.EmulatorHandle = 0


    If Not (sessioneEXTRA Is Nothing) Then
        Call SetParent(sessioneEXTRA.WindowHandle, oldParent)

        sessioneEXTRA.CloseEx (1 + 8) ' No prompt, No save
        sessioneEXTRA.WaitForDisconnect

        Set sessioneEXTRA = Nothing
    End If


    If Not (sessioniEXTRA Is Nothing) Then
        Set sessioniEXTRA = Nothing
    End If

    If Not (sysEXTRA Is Nothing) Then
        Set sysEXTRA = Nothing
    End If

    TerminateExtraObject = True
End Function


rivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)


 Select Case UnloadMode
            Case 0:
                ' User used the Control Box Menu in the upper
                ' left corner or the "X" in the upper right
                ' corner of the form. If this is one of many
                ' forms, I send it back to the form that called it.
                ' One way in, one way out.
                Call TerminateExtraObject(ExtraEvents1, Frame1)

            Case 1:
                ' Some other code within this application is causing
                ' the shutdown. Usually a routine in a BAS module.
                 Call TerminateExtraObject(ExtraEvents1, Frame1)
            Case 2:
                ' Windows session is closing (i.e. Start, Shut Down)
                Call TerminateExtraObject(ExtraEvents1, Frame1)
            Case 3:
                ' Task Manager is closing this application. (i.e.
                ' Ctrl+Alt+Del, End Task)
                Call TerminateExtraObject(ExtraEvents1, Frame1)
            Case 4:
                ' The MDI parent form is closing.
                Call TerminateExtraObject(ExtraEvents1, Frame1)
        End Select

On Error Resume Next

End Sub

Private Sub Form_Unload(Cancel As Integer)
'MsgBox "hello"
'Sleep 5000

On Error Resume Next
End Sub


 
Old September 15th, 2006, 09:48 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Just a thought—try:
Code:
Private Sub Form_Unload(Cancel As Integer)

    On Error GoTo Er

    DoEvents    <——————————<<<

Rs: Exit Sub

Er: MsgBox Err.Number & ",  """ & Err.Description & """"
    Resume Rs

End Sub
and report what happens.
 
Old September 15th, 2006, 01:21 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

there is a big difference between a msgbox and the sleep function: the sleep stops the current thread (and therefore it is highly discoraged to use it in a GUI application), the msgbox allows a series of "safe" events to be processed (like a Timer event)
I would look into the Close and WaitForDisconnect methods, to see if they leave something open anyway, if you have access to the code. Otherwise check if the WaitForDisconnect is really what what you want, or you have to call another method. Keep in mind that the SetParent is not a safe API, because it changes the the ownership of the windows whithout VB knowing it.

As a possible solution, I would add a Timer (half a second?), and in the queryUnload I would cancel the unload event, call the terminateExtraObject, and enabled the Timer. In the Timer event I would call unload me (and set a variable that you can check in the QueryUnload, to let you know that it is now time to close for good. The lag of the time should be enough for processing all events (you have to experiment with the time interval)

Buona fortuna

Marco
 
Old September 19th, 2006, 10:27 AM
Registered User
 
Join Date: Sep 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to orfano
Default

Thanks Marco for the hint. I am not sure if i followed your hints correctly because when I enable the timer it then never goes into the timer function. I did use a range of time from half second to 5 sec.

I am not a VB programmer so i may be doing something worng. I am pasting here my code. thnanks and cheers:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)




    ' Form_Unload (1)
    Call TerminateExtraObject(ExtraEvents1, Frame1)
    Timer1.Enabled = True

If flag = True Then

    ExtraEvents1 = Nothing

End If

End Sub

Private Sub MkSessExtra_Click()
Call InitExtraObject(ExtraEvents1, Frame1)
End Sub

Private Sub Form_Load()

    FileSessione3270 = App.Path & "\3270.edp"
    ShortNameSessione3270 = "A"
    flag = True

End Sub

Private Sub SganciaSess_Click()
Call TerminateExtraObject(ExtraEvents1, Frame1)
End Sub


Private Sub Timer1_Timer()
 Unload Me
 flag = True
End Sub

 
Old September 19th, 2006, 12:51 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

queryUnload(...
if not flag then
flag = true
call terminate...
canvel = 1 ' cancel the unload event
endif
 
Old September 20th, 2006, 03:02 AM
Registered User
 
Join Date: Sep 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to orfano
Default

Thnaks a Million Marcos an Brian for the hints!

Marcos last help in my code did the trick.
Thanks also marcos for the explanations given.

Whenever you come over Ireland give me a shout so we can get some pints in the pub.

cheers!!

 
Old September 20th, 2006, 01:10 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by orfano
Whenever you come over Ireland give me a shout so we can get some pints in the pub.
you are welcome. Ireland is not in my usual route from California to Italy, but I love Irish beer, so do aspect a call from me in the future! And if you want to try some of my local breweries (like
http://www.pyramidbrew.com/home.php
http://www.sierranevada.com/ or my favourite
http://www.avbc.com/main.html
nothing like yours, but worth a try) just give me a buz

Regards,
Marco





Similar Threads
Thread Thread Starter Forum Replies Last Post
closing connections - application pooling goingmad Classic ASP Databases 0 September 29th, 2005 08:20 AM
closing connections - application pooling goingmad SQL Server ASP 0 September 29th, 2005 07:19 AM
VB not closing goLynxgo XML 0 January 26th, 2005 05:44 PM
My VB 6 crashes saikrishnan Beginning VB 6 2 January 18th, 2005 12:22 PM
VB 5 crashes Alkap Beginning VB 6 1 October 24th, 2003 10:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.