|
-
Aug 11th, 2005, 12:48 AM
#1
Thread Starter
Frenzied Member
Getting out of a running loop[Resolved]
Well here is my current loop:
VB Code:
Dim Check As Boolean
For X = 0 To List1.ListCount - 1
For i = 0 To List2.ListCount - 1
user.Caption = List2.List(i)
pass.Caption = List1.List(X)
Check = LoginCheck(List2.List(i), List1.List(X))
If Check = False Then
Status.Caption = "Getting next user"
End If
Next i
Next X
It's length in time can be anywhere from 5 seconds to an hour, so it's neccesary to have one. Anyways, how would I exit this loop via a command button while it's running? I can't really think of a way to notify it to exit the loop. Any suggestions/ideas?
Last edited by Inuyasha1782; Aug 11th, 2005 at 04:19 AM.
Age - 15 ::: Level - Advanced
If you find my post useful please ::Rate It::

-
Aug 11th, 2005, 12:52 AM
#2
Re: Getting out of a running loop
Why don't you use a Do... Loop instead? You could set a condition in a Do... Loop.
-
Aug 11th, 2005, 12:55 AM
#3
Re: Getting out of a running loop
I tried a quick test and this seems to work...
VB Code:
Option Explicit
Dim NoMore As Boolean
Private Sub Command1_Click()
NoMore = True
End Sub
Private Sub Command2_Click()
Test
End Sub
Private Sub Test()
Dim a As Double
For a = 0 To 100000000
DoEvents
If NoMore = True Then
MsgBox "Stop"
Exit For
End If
Next
MsgBox "Finish"
End Sub
-
Aug 11th, 2005, 01:14 AM
#4
Hyperactive Member
Re: Getting out of a running loop
Might it be sensible, rather than simply a command button, to have a progress bar with a command button (labelled Cancel) next to it, so the user can see whether it is worth interrupting or not. You'd then use dee-u's approach.
Another thing you could do is to have x and i as globals and the loop as for x = x to List1.ListCount - 1 etc, and the Cancel button's caption could change to Resume on clicking and launch the sub again to complete filling the listboxes.
-
Aug 11th, 2005, 01:16 AM
#5
Re: Getting out of a running loop
Use timers with do events.
-
Aug 11th, 2005, 01:18 AM
#6
Re: Getting out of a running loop
together with the flag like dee u said of course to really make sure that the process is stopped WHILE the loop is still running.. 'cause if not.. I doubt other events like the click will get processesed..
-
Aug 11th, 2005, 01:22 AM
#7
Re: Getting out of a running loop
ehehehe.. sorry didn't read and check dee-u's posted code.
-
Aug 11th, 2005, 02:25 AM
#8
Thread Starter
Frenzied Member
Re: Getting out of a running loop
Im confused, how/where would I implement that into my code?
Age - 15 ::: Level - Advanced
If you find my post useful please ::Rate It::

-
Aug 11th, 2005, 02:35 AM
#9
Re: Getting out of a running loop
yup.. dee-u did not initialize his global variable NoMore...
VB Code:
Option Explicit
Dim DoTask As Boolean
Private sub Form_Load()
DoTask = true
end sub
'First Button button click event
Private Sub Command1_Click()
'set the flag to do the task to false to stop doing the flag
DoTask = false
End Sub
'Second Button click event
Private Sub Command2_Click()
'call loop here
call subDoLoop()
End Sub
Private Sub subDoLoop()
Dim a As Double
For a = 0 To 100000000
If Not DoTask Then
MsgBox "Process Cancelled"
Exit For
End If
'Do your processing here... :)
' Do events returns control to windows to allow processing of other windows commands one of which is possible the click button to pause... :)
DoEvents
Next
MsgBox "Finish"
End Sub
changed code to make it clearer. 
and added some comments.
-
Aug 11th, 2005, 02:38 AM
#10
Re: Getting out of a running loop
 Originally Posted by oceanebelle
yup.. dee-u did not initialize his global variable NoMore...
VB Code:
Option Explicit
Dim DoTask As Boolean
Private sub Form_Load()
DoTask = true
end sub
'First Button button click event
Private Sub Command1_Click()
'set the flag to do the task to false to stop doing the flag
DoTask = false
End Sub
'Second Button click event
Private Sub Command2_Click()
'call loop here
call subDoLoop()
End Sub
Private Sub subDoLoop()
Dim a As Double
For a = 0 To 100000000
If Not DoTask Then
MsgBox "Process Cancelled"
Exit For
End If
'Do your processing here... :)
' Do events returns control to windows to allow processing of other windows commands one of which is possible the click button to pause... :)
DoEvents
Next
MsgBox "Finish"
End Sub
changed code to make it clearer. 
and added some comments.
I did not mind initializing the variable anymore since boolean's default is false.
-
Aug 11th, 2005, 02:40 AM
#11
Re: Getting out of a running loop
so you have 2 buttons... one for cancelling and one for executing your code...
btw.. where is your code located?
-
Aug 11th, 2005, 02:42 AM
#12
Re: Getting out of a running loop
 Originally Posted by dee-u
I did not mind initializing the variable anymore since boolean's default is false. 
It would help to make the code more readable ... it is good practise to initialize naman eh. bad ang hindi.
-
Aug 11th, 2005, 02:44 AM
#13
Re: Getting out of a running loop
Have you declared GLOBAL variablies?
-
Aug 11th, 2005, 02:47 AM
#14
Re: Getting out of a running loop
 Originally Posted by dglienna
Have you declared GLOBAL variablies?
sorry wrong term..
global variable for the module... module variables??? isn't it the same?
-
Aug 11th, 2005, 02:56 AM
#15
Re: Getting out of a running loop
There have been a few things that have been left out in the past. I wanted to clarify things before I started trying to help him again.
-
Aug 11th, 2005, 03:14 AM
#16
Re: Getting out of a running loop
 Originally Posted by dglienna
There have been a few things that have been left out in the past. I wanted to clarify things before I started trying to help him again.
I'm intrigued... May I know that?
-
Aug 11th, 2005, 03:21 AM
#17
Thread Starter
Frenzied Member
Re: Getting out of a running loop
I still don't get it >_< I don't get how I would add that into my code without killing my other loop's. Like im not sure how to exactly add it without all my other loops stopping for that loop to go. It's kind of hard to explain, just try it and you'll see.
Age - 15 ::: Level - Advanced
If you find my post useful please ::Rate It::

-
Aug 11th, 2005, 03:25 AM
#18
Re: Getting out of a running loop
 Originally Posted by dee-u
I'm intrigued... May I know that? 
me too
-
Aug 11th, 2005, 03:26 AM
#19
Re: Getting out of a running loop
 Originally Posted by Inuyasha1782
I still don't get it >_< I don't get how I would add that into my code without killing my other loop's. Like im not sure how to exactly add it without all my other loops stopping for that loop to go. It's kind of hard to explain, just try it and you'll see.
I dunno where you place the code really... so cannot imagine nor try it...
-
Aug 11th, 2005, 03:28 AM
#20
Re: Getting out of a running loop
I've had programmed a program that will have two more than too loops supposedly may work at any given time.... and well it worked fine... will your loops may execute at the same time... at any given time????
-
Aug 11th, 2005, 03:30 AM
#21
Re: Getting out of a running loop
Well, you know how the flow of your program works so it's up to you where or how to do it, we gave guidance on how to do it so base it from there...
-
Aug 11th, 2005, 03:31 AM
#22
Thread Starter
Frenzied Member
Re: Getting out of a running loop
What? o_0 Like I said it's hard to explain. What my loop does:
The first for will take an item from one listbox, and run a check with every item in the second listbox. Once the second listbox is empty, the second loop kicks in and moves to the next item in the first lisbox, and repeats the whole process.
Also it's not a matter of my decision, as the places I have attempted to place it does not cooperate with any of my previous loops.
Age - 15 ::: Level - Advanced
If you find my post useful please ::Rate It::

-
Aug 11th, 2005, 03:35 AM
#23
Re: Getting out of a running loop
 Originally Posted by Inuyasha1782
What? o_0 Like I said it's hard to explain. What my loop does:
The first for will take an item from one listbox, and run a check with every item in the second listbox. Once the second listbox is empty, the second loop kicks in and moves to the next item in the first lisbox, and repeats the whole process.
Also it's not a matter of my decision, as the places I have attempted to place it does not cooperate with any of my previous loops.
You can't just randomly place codes in your codes (and see if it works from there)... YOU'VE got to know what you are doing... in that, you would know where to place the new code. 
If you yourself, don't know about it that how can we even help?
-
Aug 11th, 2005, 03:39 AM
#24
Thread Starter
Frenzied Member
Re: Getting out of a running loop
Which is why I said I am still confused..
I know what my code does, I just explained it to you above, and I know how it works too. Personally I don't think I can use that to stop it, since all my previous loops will be stopped no matter which way I attempt to put it. Since basicly the only logical place to put it is before all my loops, because placing it anywhere after them will cause it to be neglected until the other loop's needs are met. If I place it infront, that loop will just keep going until it's met and everything else will be neglected. If I call it from a sub, same effect.
So I do know my code, I was only asking if anyone knew a way as I personally don't think this will work.
Age - 15 ::: Level - Advanced
If you find my post useful please ::Rate It::

-
Aug 11th, 2005, 03:48 AM
#25
Re: Getting out of a running loop
dee-u's solution will work.. even used it in mine. ...*sigh*
oh well.. good luck
-
Aug 11th, 2005, 03:54 AM
#26
Thread Starter
Frenzied Member
Re: Getting out of a running loop
Heh thanks, I guess. Well im not trying to sound by what you may call a "leecher", truly I have attempted to implement it in many ways, and I just used the last one right now and it didn't work. I just think it's the way my loop's run, if it worked for your project, that's nice, but I am using a completely different function behind all of this as well. I'll keep trying, although that was my last idea, I just think there is another, not neccisarily better, way of doing this.
Age - 15 ::: Level - Advanced
If you find my post useful please ::Rate It::

-
Aug 11th, 2005, 04:09 AM
#27
Re: Getting out of a running loop
This might work...
VB Code:
Option Explicit
Dim NoMore As Boolean
Private Sub Command1_Click()
NoMore = True
End Sub
Private Sub Command2_Click()
Test
End Sub
Private Sub Test()
Dim Check As Boolean
For x = 0 To List1.ListCount - 1
For i = 0 To List2.ListCount - 1
DoEvents
If NoMore = True Then
MsgBox "Stop"
Exit Sub
End If
User.Caption = List2.List(i)
pass.Caption = List1.List(x)
Check = LoginCheck(List2.List(i), List1.List(x))
If Check = False Then
Status.Caption = "Getting next user"
End If
Next i
Next x
End Sub
-
Aug 11th, 2005, 04:18 AM
#28
Thread Starter
Frenzied Member
Re: Getting out of a running loop
Hmm, not at first since my function messes some of that up, but I fixed it up a little differently, thanks
Age - 15 ::: Level - Advanced
If you find my post useful please ::Rate It::

-
Aug 11th, 2005, 04:24 AM
#29
Re: Getting out of a running loop[Resolved]
Glad to help.
-
Aug 11th, 2005, 07:22 PM
#30
Re: Getting out of a running loop
 Originally Posted by Inuyasha1782
Hmm, not at first since my function messes some of that up, but I fixed it up a little differently, thanks 
good for you
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
|