Hello,

is there a way I can pause and then Resume code execution by clicking a "Pause" /"Resume" button using Application.DoEvent, How? An example please.

Try something like this:

Private Sub btnLoop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoop.Click
        Do While Not Done
            My.Application.DoEvents()
            Do While OverDone
                My.Application.DoEvents()
            Loop
        Loop
        Started = False
        Done = False
        OverDone = False
    End Sub

  
    Private Sub btnToggleDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToggleDone.Click
        If Started Then
            Done = Not Done
        End If
    End Sub

    Private Sub btnToggleOverdone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToggleOverdone.Click
        If Started Then
            If Not Done Then
                OverDone = Not OverDone
            End If
        End If

    End Sub

The inner loop just waits untill the OverDone is toggled.

Hello,
thank you for your reply. I have some questions. In my code I am retriving binary files from a database and all its related information from other tables to send them to another database using webservices. My outer loop is my records from the database, what is the inner loop?
dose btnToggleDone and btnToggleOverdone my pause and resume buttons?

thanks

Hello,
thank you for your reply. I have some questions. In my code I am retriving binary files from a database and all its related information from other tables to send them to another database using webservices. My outer loop is my records from the database, what is the inner loop?
dose btnToggleDone and btnToggleOverdone my pause and resume buttons?

thanks

No btnToggleDone would not be used in your program as that is the outer loop (just so I could run it and check it). btnToggleOverdone is your pause and resume button. One way to handle this is have the button lable say "Pause" when it is false and "Resume" when it is true.

Private Sub btnTogglePauseContinue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTogglePauseContinue.Click
If Started Then
If Not Done Then
OverDone = Not OverDone
End If
If OverDone = True Then
btnToggleOverdone.Text = "Resume"
Else
btnToggleOverdone.Text = "Pause"
End
End If
End Sub

Private Sub btnTogglePauseContinue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTogglePauseContinue.Click
        If Started Then
            If Not Done Then
                OverDone = Not OverDone
            End If
            If OverDone = True Then
                btnToggleOverdone.Text = "Continue"
            Else
                btnToggleOverdone.Text = "Pause"
            End
        End If
End Sub

The last post did not have the code tags in the edit so I am posting the same code but indented (easier to read).

Hi,

You can use System.Threading.Thread.Sleep(200) in the inner loop along with Application.DoEvents() to avoid CPU overhead.

Loren Soth

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.