locked
How to terminate or exit a for loop when the user clicks on stop button RRS feed

  • Question

  • Actually my problem is to stop a loop when i click on stop button.
    example:i have two buttons 'start' and 'stop'
    in start buttom i wrote a for loop as

    dim i as integer
    For i=1 To 100000
    print i
    Next

    when i click on start buuton it prints 'i' value up tp 100000.

    my question is when i click on 'Stop' button the for loop has to terminate or Exit from the  loop and should stops the execution.
    Is it possible to termianate or Exit the 'for loop'

    PS.Shakeer Hussain Hyderabad
    Thursday, March 18, 2010 6:26 PM

Answers

All replies

  • Actually my problem is to stop a loop when i click on stop button.
    example:i have two buttons 'start' and 'stop'
    in start buttom i wrote a for loop as

    dim i as integer
    For i=1 To 100000
    print i
    Next

    when i click on start buuton it prints 'i' value up tp 100000.

    my question is when i click on 'Stop' button the for loop has to terminate or Exit from the  loop and should stops the execution.
    Is it possible to termianate or Exit the 'for loop'

    PS.Shakeer Hussain Hyderabad
    • Merged by Liliane Teng Friday, March 19, 2010 2:18 AM a duplicate thread
    Thursday, March 18, 2010 6:29 PM
  • Create a boolean variable, inside your cancel button, set cancel=true

    dim cancel as boolean=False

    For i =1 to 100000
    if cancel=true
    exit for
    end if
    print i
    Next

    vb6 forum : http://www.vbforums.com/forumdisplay.php?forumid=1

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Thursday, March 18, 2010 6:32 PM
  • Hi kaymaf,
    In the same way,i want exit in while loop.
    How plz write a sample code
    PS.Shakeer Hussain Hyderabad
    Thursday, March 18, 2010 6:37 PM
  • Public Class Form1
        Dim continueLooping As Boolean = True
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            continueLooping = False
        End Sub
    
        Private Sub LoopMethod()
            Dim i As Integer = 0
            While (Me.continueLooping)
                i += 1
                Print(i)
                If (i = 100000) Then
                    Me.continueLooping = False
                End If
            End While
        End Sub
    End Class


    Mark the best replies as answers. "Fooling computers since 1971."
    Thursday, March 18, 2010 6:45 PM
  • Try this
    Dim i As Integer
    Dim cancel as Boolean=False
    Do Until i < 100000
        i = i + 1
     Print i
    if cancel=true
    Exit Do
     End if
    Loop 

    Take look at this link for vb6 loop
    http://www.vb6.us/tutorials/understanding-do-and-while-loops


    Also, this is VB.NET forum which does not support VB6. For Vb6 issue, you can try this link below

    vb6 forum : http://www.vbforums.com/forumdisplay.php?forumid=1

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Thursday, March 18, 2010 7:09 PM
  • Hi kaymaf and Rudedog2
    I tested you code by taking two buttons start and stop.

    when i click on 'start' buttin to start a loop it executing a loop.when i try to terminate or stop a loop by clicking on 'stop' button,the stop button does not take any action or click event untill the loop completly executed.

    plz test it.you will get idea about it.

    waitng for you reply

    PS.Shakeer Hussain Hyderabad
    Thursday, March 18, 2010 7:11 PM
  • Add this line inside of the loops.

    Application.DoEvents.
    Mark the best replies as answers. "Fooling computers since 1971."
    Thursday, March 18, 2010 7:19 PM
  • Hi Rudedog2

    you had solved my problem
    VERY VERY VERY VERY Thank you.
    IS Application.DoEvents  will works in vb6.0 or not.i test above code in vb.net,it is woking.

    Actually in my project(win form-start Scan button) i have while loop.in while loop i had written a code to SCAN an object in X-Axis,Y-Axis and Theta Aixs.while we kept in scan mode it scans every particle in 3 directions, i am using while loop.it takes 2 to 3 days to complete a SCAN of an object.in the middle user clicks on 'stop scan' button,the while loop has to terminate and stop the scan. 

    and one more thing during this process in while loop,while the user minimizing the form and opened another programme or word document the win form will stuck or hangs.

    How to avoid without hanging a win-form and user can open any programme normally.
    waiting for your reply

    And also thanks to Kaymaf
    PS.Shakeer Hussain Hyderabad
    Thursday, March 18, 2010 7:36 PM

  • vb6 forum : http://www.vbforums.com/forumdisplay.php?forumid=1

    kaymaf




    That guy's wise.

    Mark the best replies as answers. "Fooling computers since 1971."
    Thursday, March 18, 2010 7:40 PM
  • and one more thing during this process in while loop,while the user minimizing the form and opened another programme or word document the win form will stuck or hangs.

    How to avoid without hanging a win-form and user can open any programme normally.
    waiting for your reply

    And also thanks to Kaymaf
    PS.Shakeer Hussain Hyderabad


    You really need to throw that entire process into a background worker (which is what I would have suggested to start with even with your original question).

    Give it some thought. The background worker can be daunting at first but for *most* things that you'll use it for, it's really not all that difficult.

    Good luck :)
    Thursday, March 18, 2010 7:40 PM
  • Background Worker.  Take a look at this thread.

    how to add a progress bar in visual studio 2005/2008 (vb.net) when ...

    Mark the best replies as answers. "Fooling computers since 1971."
    Thursday, March 18, 2010 7:43 PM

  • vb6 forum : http://www.vbforums.com/forumdisplay.php?forumid=1

    kaymaf




    That guy's wise.

    Mark the best replies as answers. "Fooling computers since 1971."

    Oh, he's using VB6?

    I didn't pick up on that - I'm out of my element then. :-o
    Thursday, March 18, 2010 7:43 PM
  • Hi Smith,
    First my importance is ,i need a code to terminate a while loop.

    seocnd importance  is hanging a from.

    if we post  a question step by step then it is easy to understand to me and others also.


    PS.Shakeer Hussain Hyderabad
    Thursday, March 18, 2010 7:45 PM

  • Also, this is VB.NET forum which does not support VB6. For Vb6 issue, you can try this link below

    vb6 forum : http://www.vbforums.com/forumdisplay.php?forumid=1

    kaymaf.  That guy's wise.

    Mark the best replies as answers. "Fooling computers since 1971."
    Thursday, March 18, 2010 7:46 PM
  • If you're using VB6 then I'm sorry to say that I can't help. This forum is about VB.NET and I honestly don't know the differences but I understand they're a good bit different.
    Thursday, March 18, 2010 7:47 PM
  • Hi

    just let me know Application.DoEvents will works or not in VB6.

    if not works in VB6 then what code i have to write instead of writtng Application.DoEvents

    Plz help it.......Its important for me.....


    PS.Shakeer Hussain Hyderabad
    Thursday, March 18, 2010 7:55 PM
  • Hi Smith,
    First my importance is ,i need a code to terminate a while loop.

    seocnd importance  is hanging a from.

    if we post  a question step by step then it is easy to understand to me and others also.


    PS.Shakeer Hussain Hyderabad


    The command I gave for .NET has the same name as the one in VB6, DoEvents.

    What is DoEvents?

    Mark the best replies as answers. "Fooling computers since 1971."
    Thursday, March 18, 2010 7:55 PM
  • Hi Rudedog2
    Thank you very much,you saved me.

    Plz give me solution for avoiding a win-from  stuck or hanging.

    Why its happening like that.This is my biggest bug in my project..
    waiting for reply

    PS.Shakeer Hussain Hyderabad
    Thursday, March 18, 2010 7:58 PM
  • If you're using VB6 then I'm sorry to say that I can't help. This forum is about VB.NET and I honestly don't know the differences but I understand they're a good bit different.


    Yes, I think he was already pointed to the VB 6.0 link in another thread.

    In any event (no pun intended), he can use DoEvents, a Timer or the GetQueueStatus API function call so that the Stop button Click event can be processed and the loop terminated.

    Paul ~~~~ Microsoft MVP (Visual Basic)
    Thursday, March 18, 2010 7:59 PM
  • Hi Rudedog2
    Background Worker. Take a look at this thread. how to add a progress bar in visual studio 2005/2008 (vb.net) when ... --------------------------------------------------------------------------------
    I saw above thread.can i use the same code in vb6.
    there are so many responses.which response code i have to use.
    PS.Shakeer Hussain Hyderabad
    Thursday, March 18, 2010 8:07 PM


  • The command I gave for .NET has the same name as the one in VB6, DoEvents.

    What is DoEvents?

    This time, take a look at the link.
    Mark the best replies as answers. "Fooling computers since 1971."
    Thursday, March 18, 2010 8:10 PM
  • Not about Doevents.

    about the Link<a>how to add a progress bar in visual studio 2005/2008 (vb.net) when ... </a>
    PS.Shakeer Hussain Hyderabad
    Thursday, March 18, 2010 8:12 PM
  • There are complete examples of adding a ProgressBar and a Label to Windows Form that are updated by a BackgroundWorker at the link, written in VB, C#, and C++.  What more do you want?  If you want me to write an example, I won't.  I already wrote up 3 examples that thoroughly demonstrate how to use a BackgroundWorker. 

    This forum is not for VB6.  Stop asking for VB.NET solutions, then asking for a translation to VB6.  You have been directed where to go for the help that you seek.

    Happy Coding.

     


    Mark the best replies as answers. "Fooling computers since 1971."
    • Proposed as answer by Rudedog2 Friday, March 19, 2010 12:54 PM
    Friday, March 19, 2010 12:53 PM
  • I am unable to stop the loop and application not at all allowing to Press the Stop button.

    It seems like Hung, any advise ?

    Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
            btnStop.Enabled = True
            btnSelectFile.Enabled = False
            btnStart.Enabled = False
            btnStop.Focus()

            Dim strFileName As String = txtFileName.Text.ToString
            Dim strLineText As String

            If System.IO.File.Exists(strFileName) = True Then
                Dim objReader As New System.IO.StreamReader(strFileName)

                While objReader.Peek() <> -1 And stopclick = False

                    strLineText = objReader.ReadLine()
                    MsgBox(strLineText, MsgBoxStyle.Information)
                    Application.DoEvents()
                    Thread.Sleep(My.Settings("strDelay") * 1000)
                    'System.Diagnostics.Process.Start(My.Settings("strFireFoxLocation"), strLineText)


                End While


            End If
        End Sub


        Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
            stopclick = True
            btnSelectFile.Enabled = True
            btnStart.Enabled = True
            btnStop.Enabled = False
        End Sub


    Raman Katwal


    Please remember to mark the replies as answers if they help and unmark them if they provide no help


    • Edited by Raman_Katwal Wednesday, October 15, 2014 12:02 PM
    Wednesday, October 15, 2014 12:00 PM
  • Simple and exactly what I was looking for. Thank you!
    Thursday, January 22, 2015 11:29 PM