Share via


Execute While loop when button pressed

Question

Wednesday, July 27, 2016 9:39 AM

Dear all,

I am using visual basic 2010 for programming. I have 2 buttons "start" and "stop".

when start button pressed ; while loop should keep executing unless stop button being pressed

When start button pressed while loop should executed. In richTextbox first has to print "helllo" and then "welcome". The printing action change should be every 3S to 5S.I could see change print "hello" and welcome for every 3S-5S. during this interval stop is pressed exist the while loop()

In below code.I could able to change state of start and stop 

but could not able to print in richtextbox one after the other

start button to start while loop and stop button to stop the while loop

when button has pressed richtextbox has to print "hello" -> delay(1000)-> print "welcome" ->delay(1000)-> print "hello" ->delay(1000)-> print "welcome" -> and so on.If button stop pressed it should stop printing "welcome" and "hello" 

Option Explicit On



Public Class Form1
    Declare Sub Sleep Lib "kernel32" (ByVal milliseconds As Long)
    Dim stopclick As Boolean = False
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_click.Click


        While True
            Application.DoEvents()
            sleep_program()
            RichTextBox1.Text = "hello"
            sleep_program()
            RichTextBox1.AppendText(RichTextBox1.Text)
            ' Perform the while statements
            If stopclick Then
                stopclick = False
                Exit While
            End If
        End While

        


    End Sub


    Private Sub Button_stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_stop.Click
        stopclick = True
        RichTextBox1.Text = "Button stop pressed"
    End Sub

    Sub sleep_program()
        Threading.Thread.Sleep(3000)
    End Sub
End Class

AMPS12

All replies (4)

Wednesday, July 27, 2016 10:40 AM âś…Answered

I could able to receive data one after the other with below line of code

Option Explicit On



Public Class Form1
    Dim stopclick As Boolean = True

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_click.Click
        While True
            Timer1.Interval = 3000
            Timer1.Start()
            RichTextBox1.AppendText("Timer Started" & vbCrLf)
            If stopclick Then
                stopclick = False
                Exit While
            End If
        End While


    End Sub


    Private Sub Button_stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_stop.Click
        Timer1.Stop()
        stopclick = True
        RichTextBox1.AppendText("Timer Stopped" & vbCrLf)

    End Sub


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        RichTextBox1.AppendText("Hello" & vbCrLf)
    End Sub
End Class

AMPS12


Wednesday, July 27, 2016 9:59 AM

You should run the loop in a separate thread. However to access controls on the Form from a separate thread you will need to use something such as a delegate sub to update those controls as the delegate sub will be on the same thread as the Form.

Or use a BackgroundWorker and use its events. The DoWork event could run the loop. In the loop use System.Threading.Thread.Sleep(3) for 3 milliseconds to sleep the thread. Use a global boolean that a button click event can set to true or false. Have the loop check if the global boolean is set to whatever value makes the loop stop so the BackgroundWorker will exit. Update the RichTextBox by having the DoWork event call the ProgressChanged event as the ProgressChanged event runs on the same thread as the Form. Have the ProgressChanged event update the RichTextBox.

La vida loca


Wednesday, July 27, 2016 10:06 AM

when button has pressed richtextbox has to print "hello" -> delay(1000)-> print "welcome" ->delay(1000)-> print "hello" ->delay(1000)-> print "welcome" -> and so on. If button stop pressed it should stop printing "welcome" and "hello"

Do not use a loop for this.

When the start button is pressed, start a timer with a tick period of 1000ms.   Each time the timer ticks, in the timer tick event handler, print a message.  Use a form-level variable to indicate which message is to be printed, updating it each time so you flip between messages.

When the stop button is pressed, stop the timer.

See here:  http://www.technologyuk.net/computing/software_development/event_driven_programming.shtml


Wednesday, July 27, 2016 12:48 PM

I could able to receive data one after the other with below line of code

Please don't mark your own response as the answer.

Remove the While / End While loop - you only want to start the timer once.  

Initialize stopclick to false, not true (the stop button has not been pressed when the program first starts).  Move the test for stopclick from the start button click event handler to the timer tick event handler.   Or, discard everything to do with stopclick.