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.
Printable View
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.
why dont u pop-up a msgbox when the button is pressed?
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?
Try somthing like this:
VB Code:
Private stopFlag As Boolean Private counter As Integer = 0 Private Sub cmdLoop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoop.Click stopFlag = False cmdStop.Focus() While counter < 2000000 counter += 1 Application.DoEvents() If stopFlag = True Then MsgBox("stop") Exit While End If End While End Sub Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click stopFlag = True End Sub
regard j
Something like this:
VB Code:
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click blnStop = False blnPause = False btnStart.Enabled = False MyLoop() End Sub Private Function MyLoop() Dim i As Int16 Dim j As Int16 i = 0 j = 1 While i < j i = i + 1 j = j + 1 Label1.Text = i.ToString Me.Refresh() Application.DoEvents() Do While blnPause Application.DoEvents() If blnStop Then Exit Function Loop If blnStop Then Exit Function End While End Function Private Sub btnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPause.Click If blnPause Then btnPause.Text = "Pause" Else btnPause.Text = "Resume" End If blnPause = Not blnPause End Sub Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click blnStop = True btnPause.Text = "Pause" btnStart.Enabled = True End Sub
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.
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:
Public myThreadDelegate As New ThreadStart(AddressOf SUB_WHICH_CONTAINS_LOOP) Public myThread As New Thread(myThreadDelegate)
on the button start event stick
VB Code:
myThread.Start()
on the pause button event stick
VB Code:
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:
If myThread.ThreadState = ThreadState.Suspended Then myThread.resume() else myThread.start() 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
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?
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