|
-
May 14th, 2004, 07:41 AM
#1
Thread Starter
Frenzied Member
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.
-
May 14th, 2004, 07:50 AM
#2
Fanatic Member
why dont u pop-up a msgbox when the button is pressed?
-
May 14th, 2004, 08:43 AM
#3
Hyperactive Member
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)
-
May 14th, 2004, 08:47 AM
#4
Addicted Member
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
-
May 14th, 2004, 09:08 AM
#5
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
-
May 14th, 2004, 09:52 AM
#6
PowerPoster
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.
-
May 14th, 2004, 10:24 AM
#7
Fanatic Member
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
on the pause button event stick
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
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
-
May 15th, 2004, 09:18 PM
#8
New Member
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?
-
May 16th, 2004, 02:45 AM
#9
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|