Resolved Output from Do-Loop

Plan_OK

Member
Joined
Aug 21, 2023
Messages
24
Programming Experience
1-3
I have an infinite Do - Loop that invokes an audio file. There is a Button control on the screen that, when pressed, causes the program flow to continue.
Changing the flow, however does not interrupt the loop which continues in the background still executing the sound file which cannot be interrupted.
I have temporarily replaced the Do-Loop loop with a For-Next loop with fixed iteration, however, not being able to control the number of repetitions, but it is not the best.

I attach part of the listing. Thank you.
VB.NET:
        For volte = 1 To 5 ' prec: Do
            If chkSuono.Checked Then Parla("tang")
            For dimChar = 10 To 30
                lblPrimaRegione.Font = New Font(lblPrimaRegione.Font.Name, dimChar, FontStyle.Bold)
                Ritardo(22)
            Next
            For dimChar = 30 To 10 Step -1
                lblSecondaRegione.Font = New Font(lblSecondaRegione.Font.Name, dimChar, FontStyle.Bold)
                Ritardo(22)
            Next
            For dimChar = 30 To 10 Step -1
                lblPrimaRegione.Font = New Font(lblPrimaRegione.Font.Name, dimChar, FontStyle.Bold)
                Ritardo(22)
            Next
            For dimChar = 10 To 30
                lblSecondaRegione.Font = New Font(lblSecondaRegione.Font.Name, dimChar, FontStyle.Bold)
                Ritardo(22)
            Next
        Next ' prec: Loop


    Public Sub Ritardo(ByVal TIME_RITARDO As Integer)
        Dim StartTime As Date = Now
        Do
            Application.DoEvents()
        Loop Until (Now - StartTime).TotalMilliseconds > TIME_RITARDO
    End Sub

    Public Sub Parla(file)
        stPercorsoMusica = Application.StartupPath() & "\" & file & ".mp3"
        mciSendString("close " & stAliasMusica, CStr(0), 0, 0)
        mciSendString("Open " & Chr(34) & stPercorsoMusica & Chr(34) & " alias " & stAliasMusica, CStr(0), 0, 0)
        mciSendString("play " & stAliasMusica, CStr(0), 0, 0)
    End Sub
I can intercept the left click with:
VB.NET:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If GetAsyncKeyState(Keys.LButton) <> 0 Then
            ' ????
            ' ????
        End If
    End Sub

But I don't know how to control the exit from the loop.

How to intercept the button click press from within the loop to interrupt it?

Thank you.
 
You could have a form level variable that is toggled when the button is clicked and in your Timer check the state of the form level variable and exit the loop. Here is an example where I have a button that starts counting and another button that stops it:

VB.NET:
    Dim StopCounting As Boolean
    Dim currentCount As Int32

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        Do While Not StopCounting
            currentCount += 1
            lblTimerCount.Text = currentCount.ToString
            Application.DoEvents()
        Loop

    End Sub

    Private Sub btnStartCounting_Click(sender As Object, e As EventArgs) Handles btnStartCounting.Click
        StopCounting = False
        Timer1.Enabled = True
    End Sub

    Private Sub btnStopCounting_Click(sender As Object, e As EventArgs) Handles btnStopCounting.Click
        StopCounting = True
    End Sub
 
Thank you Jdelano for your quick response.
I have tried to include in my project, according to your suggestions, the proposed interventions, I hope I interpreted everything correctly, but I must say that the result is not quite as expected. To be sure, I also started a new project with only the elements involved: same result.
I included the timer activation in the procedure before the Do loop.
Now, however, the commands contained in the loop (both audio and graphics) are all executed once.
The counter starts by declaring the timer active, but there are no other outcomes: sound and graphics are stopped.
Very strange thing (!?) the audio starts again as soon as the counting is stopped by activating the variable StopCounting = True.

The stream is already far away, though!
The desired one was to keep sound and graphics active until a click on the cmdControlloNo or cmdControlloYes button is made.


VB.NET:
    Private Sub OscillaRegioni()
        StopCounting = False
        Timer1.Enabled = True

        Do
            Parla("tang")
            For dimchar = 10 To 30
                lblPrimaRegione.Font = New Font(lblPrimaRegione.Font.Name, dimchar, FontStyle.Bold)
                Ritardo(22)
            Next
        ‘ ...   
       Loop



    Private Sub cmdControlloNo_Click(sender As Object, e As EventArgs) Handles cmdControlloNo.Click
        StopCounting = True
        Dim mess As String = "*"
        If nScelta = 2 Then

Can you kindly tell me if I made any mistake? Thank you, looking forward to your reply.
 
In order to have the loop stop looping, you need to check the value of StopCounting in the loop.

In your example, you'd need to add a line
VB.NET:
For dimchar = 10 To 30
    If StopCounting Then Exit For
    lblPrimaRegione.Font = New Font(lblPrimaRegione.Font.Name, dimchar, FontStyle.Bold)
    Ritardo(22)
    Application.DoEvents() ' required to allow the GUI to interpret the click of the stop button
Next

Additionally, if you're not using the Timer_Tick event to run your loop then there is no reason to have a timer in your project.
 
Many thanks Jdelano,
I understood your remarks and applied your suggestions.
I am very glad to have learned something I knew little about even in logic. It was much appreciated your intervention and especially your patience.
The only change that seems to me to be necessary is the fact of putting the control in the Do-Loop and not in the For-Next loop.
I did several tests to synchronize the duration of the sound file with the timer and delay times, finally all OK.
I would have other problems, but I am afraid to present them because I don't want them to be of little general interest, but for those like me who don't know the language properly, having qualified help, allows one to go on and learn more mainly with acceptable time. Is my reasoning correct?

Thank you again for your intervention, see you soon.
 
I am glad that you're getting closer to where you want your project to be, I am happy to help.
Please do post your issues, these forums are here so that those that have done these things for a while can help those that are just learning.
 

Latest posts

Back
Top