Results 1 to 9 of 9

Thread: pausing a while loop using code

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    pausing a while loop using code

    If you have a loop running and you would like to pause it using a button and start it running again from that point, how could that be done? think of it as a tape that gets a pause button pressed and unpressed.

  2. #2
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961
    why dont u pop-up a msgbox when the button is pressed?

  3. #3
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    I think your problem is how to know if button is pressed, isn't it?
    You have to use:

    Application.DoEvents()

    It forces application to serve events occurred and so the event ButtonClick will be served. Put the instruction on every cicle, or any ten turns or....as you prefer
    Was this the problem, Andy?
    Live long and prosper (Mr. Spock)

  4. #4
    Addicted Member
    Join Date
    Aug 2002
    Location
    top of the mountain
    Posts
    234
    Try somthing like this:
    VB Code:
    1. Private stopFlag As Boolean
    2.     Private counter As Integer = 0
    3.  
    4.     Private Sub cmdLoop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoop.Click
    5.         stopFlag = False
    6.         cmdStop.Focus()
    7.         While counter < 2000000
    8.             counter += 1
    9.             Application.DoEvents()
    10.             If stopFlag = True Then
    11.                 MsgBox("stop")
    12.                 Exit While
    13.             End If
    14.         End While
    15.  
    16.     End Sub
    17.     Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click
    18.         stopFlag = True
    19.     End Sub

    regard j

  5. #5
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    Something like this:

    VB Code:
    1. Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    2.         blnStop = False
    3.         blnPause = False
    4.         btnStart.Enabled = False
    5.         MyLoop()
    6.     End Sub
    7.  
    8.     Private Function MyLoop()
    9.         Dim i As Int16
    10.         Dim j As Int16
    11.         i = 0
    12.         j = 1
    13.         While i < j
    14.             i = i + 1
    15.             j = j + 1
    16.             Label1.Text = i.ToString
    17.             Me.Refresh()
    18.             Application.DoEvents()
    19.             Do While blnPause
    20.                 Application.DoEvents()
    21.                 If blnStop Then Exit Function
    22.             Loop
    23.             If blnStop Then Exit Function
    24.         End While
    25.     End Function
    26.  
    27.     Private Sub btnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPause.Click
    28.         If blnPause Then
    29.             btnPause.Text = "Pause"
    30.         Else
    31.             btnPause.Text = "Resume"
    32.         End If
    33.         blnPause = Not blnPause
    34.     End Sub
    35.  
    36.     Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
    37.         blnStop = True
    38.         btnPause.Text = "Pause"
    39.         btnStart.Enabled = True
    40.     End Sub

  6. #6
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi janis,

    I know that your code will not work without

    Appplication.DoEvents()

    but I cant see why. I would have thought that the loop would function without it, but it doesn't.

    Any ideas anyone?


    EDIT

    Sorry. I've worked it out. The Application.DoEvents() is needed to enable outside events to be recognised during the application of the loop.
    Last edited by taxes; May 14th, 2004 at 09:59 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  7. #7
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539
    Yo andy, you know the best way to do this is using threading

    Imports System.Threading

    On the form/class that contains the loop stick

    VB Code:
    1. Public myThreadDelegate As New ThreadStart(AddressOf SUB_WHICH_CONTAINS_LOOP)
    2.  
    3. Public myThread As New Thread(myThreadDelegate)

    on the button start event stick

    VB Code:
    1. myThread.Start()

    on the pause button event stick

    VB Code:
    1. myThread.Suspend()

    how u handle starting the loop again is up to you, you could on the start button have an event to check the state of the thread if paused then resume it, if not then start it

    VB Code:
    1. If myThread.ThreadState = ThreadState.Suspended Then
    2. myThread.resume()
    3. else
    4. myThread.start()
    5. end if

    This is a really nice way to handle pausing stuff, because it keeps the UI active during the pause period....
    Of course if you want to run other stuff while this thread is paused then your need to do that in another thread or just make this one a background thread......

    Syntax is hopefully correct, if not it ant going to be too far off
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  8. #8
    New Member
    Join Date
    May 2004
    Posts
    9
    Hi carlblanchard,
    I have a problem on this part:
    Public myThreadDelegate As New ThreadStart(AddressOf SUB_WHICH_CONTAINS_LOOP) <--- my sub contains parameter mysub(parameter), how can i resolve this?

  9. #9
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539
    ok this is where it gets hard...

    You cant use parameters, so this is what i do to work arround

    Public myThreadDelegate As New ThreadStart(AddressOf RunProg)
    Public myThread As New Thread(myThreadDelegate)

    imagine this is a button event

    Sub onClick blah blah (sorry just woke up)

    myThread.start()

    end

    Private Sub RunProg
    'Call your sub with the parameters here
    myParamSub(blah,blah,blah)
    end sub

    See what this will do is execute the subs (its a chain reaction) so even if you had more subs within myParamSub they will all be executed in the new thread.....

    Dynamic params will still work just assign then the myParamSub

    hope this makes sense after all its 9am sunday morning and i havnt had my coffee yet
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

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