Hi,

Does anyone give me some suggestion?
When I close the form, but some process is still running.I don't know how to handle this kind of situation. What I want to do is that stop all processes first and then close form automatically.

Private Sub Command1_Click()
    
    'Do process 1
    Call sleep(10)
    
    'Do process 2
    Call sleep(10)

End Sub
Private Sub sleep(ByVal slp As Double)
    Dim start
    
    start = Timer
    
    Do While (Timer < (slp + start)) 
        DoEvents
    Loop
End Sub

I don't understand what the relevance about your question and your posting code, but i still to answer it.

If you want to close all form and end the program then you can use END .
Unload Me just close the current form not the entire program. So any hidden form can still running.

Private Sub Command1_Click()
Unload Me
End Sub

When you use End Statement it will close all entire form, Hidden or not hidden. so any process will turning off.

Private Sub Command2_Click()
End
End Sub
commented: I think.. +3

Hi Jx Man,

sorry for my question is not clear. The following is my flow.

-Click buttom1
-valve one is ON.
-Delay 10 seconds
-valve one is OFF

When the user close "form" when the system is doing delay process loop, I would like to exit "Delay" loop, close "valve". Finally, the system close "Form".

I don't want to use "End" when the form is "Unload me" because the "valve" is already opened.

hi zawpai,,this thread already solved or not?

commented: why are you so interested in getting the thread solved ? -3
commented: don't +3

In the Unload event of the form set the valve to Close then use End to terminate the program.

Hi ChrisPadgham,

Thanks for your information.

dnk,

I solve it. I use one additional timer to check whether Unload event is trigger or not. I didn't use "End" command to close the application because we have another form is running in our whole system. I am not sure my method is correct or not. This is my sample code.

Dim Close_frm As Boolean, Start_Dis As Boolean
Dim Valve1 As Boolean, Valve2 As Boolean

Private Sub Command1_Click()
    Start_Dis = True
    
    Label1.Caption = "Open Valve 1"
    Valve1 = True

    Call sleep(4)
    
    If (Start_Dis = True) Then
        Label2.Caption = "Open Valve 2"
        Valve2 = True
    End If
    
End Sub

Private Sub sleep(ByVal slp As Double)
    Dim start
    
    start = Timer
    
    Do While ((Timer < (slp + start)))
        DoEvents
        If (Start_Dis = False) Then
            Exit Do
        End If
    Loop
End Sub

Private Sub Close_Both_Valves()
    Label1.Caption = "Close Valve 1"
    Label2.Caption = "Close Valve 2"
    Valve1 = False
    Valve2 = False
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    
    If (Start_Dis = True) Then
        Close_frm = True
        Cancel = 1
        Exit Sub
    Else
        Unload Me
    End If
    
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If (Valve1 = True) Or (Valve2 = True) Or (Start_Dis = True) Then
        Timer1.Enabled = False
        Close_Both_Valves
    End If
End Sub

Private Sub Timer1_Timer()
    Timer1.Enabled = False
    
   If (Close_frm = True) Then
        If (Start_Dis = True) Then
            Start_Dis = False
        Else
            Unload Me
            Exit Sub
        End If
    End If
    
    Timer1.Enabled = True
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.