Results 1 to 4 of 4

Thread: How to pause in the For loop condition and resume after that.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    18

    Smile How to pause in the For loop condition and resume after that.

    Hi everone,
    can anyone help me to pause in the For loop condition and resume after that by click on the desired button such as Pause button to pause the operation , resume to continues for last status.

    Thanks in advance
    Have a good day.


    <VBCODE>
    Private Sub GEN1()
    Dim A As Integer
    For A = 1 To 4
    Select Case A
    Case 1
    TextBox1.AppendText("1A" + vbCrLf)
    Call GEN2()
    Case 2
    TextBox1.AppendText("2A" + vbCrLf)
    Call GEN2()
    Case 3
    TextBox1.AppendText("3A" + vbCrLf)
    Call GEN2()
    Case 4
    TextBox1.AppendText("4A" + vbCrLf)
    Call GEN2()
    End Select
    Next A
    End Sub
    Private Sub GEN2()
    Dim B As Integer
    For B = 1 To 5
    Select Case B
    Case 1
    TextBox1.AppendText("1B" + vbCrLf)
    Case 2
    TextBox1.AppendText("2B" + vbCrLf)
    Case 3
    TextBox1.AppendText("3B" + vbCrLf)
    Case 4
    TextBox1.AppendText("4B" + vbCrLf)
    Case 5
    TextBox1.AppendText("5B" + vbCrLf)
    End Select
    Next B
    End Sub

    Private Sub RUN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click

    sugest surce code
    call gen1()

    END SUB

    Private Sub PAUSE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click

    suggest source code

    END SUB

    Private Sub RESUME_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click

    suggest source code

    END SUB

    <\vbcode>

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,437

    Re: How to pause in the For loop condition and resume after that.

    You're going to have to use two threads for this. You can execute your loop in a worker thread and then suspend and resume that thread from the main thread. Multi-threading is not something you just jumpo into and get right. Most people just jump into it and get it wrong. The sensible folk, like you, do some reading on the subject first. I suggest that you follow the Articles -> Advanced .NET link in my signature and read the section on Managed Threading.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,235

    Re: How to pause in the For loop condition and resume after that.

    There is another way to do it. Put your entire loop code into a function....ok, that's not really necessary, but it will look better. For example, call the function LoopCode(). Then have something like this:

    In the form:

    dim flag as boolean

    In the button that starts the code:

    flag = true
    LoopCode()

    In the Pause button:

    flag = false

    In the Resume button:

    flag = true
    LoopCode()


    LoopCode() will have to look something like this:

    Do while Flag
    DoEvents
    <whatever code is in the loop>
    Loop


    I have been using this code through two versions of a program, it works fine, though it is a bit more complex than this. Multithreading is much more complicated as JM stated, and it could be quicker in this situation, but this will work, and is really simple.
    My usual boring signature: Nothing

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,437

    Re: How to pause in the For loop condition and resume after that.

    Quote Originally Posted by Shaggy Hiker
    this will work, and is really simple.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width