Results 1 to 30 of 30

Thread: Getting out of a running loop[Resolved]

  1. #1

    Thread Starter
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Resolved Getting out of a running loop[Resolved]

    Well here is my current loop:

    VB Code:
    1. Dim Check As Boolean
    2. For X = 0 To List1.ListCount - 1
    3. For i = 0 To List2.ListCount - 1
    4. user.Caption = List2.List(i)
    5. pass.Caption = List1.List(X)
    6. Check = LoginCheck(List2.List(i), List1.List(X))
    7. If Check = False Then
    8. Status.Caption = "Getting next user"
    9. End If
    10. Next i
    11. 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::


  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Getting out of a running loop

    I tried a quick test and this seems to work...

    VB Code:
    1. Option Explicit
    2. Dim NoMore As Boolean
    3.  
    4. Private Sub Command1_Click()
    5.     NoMore = True
    6. End Sub
    7.  
    8. Private Sub Command2_Click()
    9.     Test
    10. End Sub
    11.  
    12. Private Sub Test()
    13.     Dim a As Double
    14.     For a = 0 To 100000000
    15.         DoEvents
    16.         If NoMore = True Then
    17.             MsgBox "Stop"
    18.             Exit For
    19.         End If
    20.     Next
    21.     MsgBox "Finish"
    22. End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4
    Hyperactive Member
    Join Date
    May 2005
    Posts
    324

    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.

  5. #5
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Getting out of a running loop

    Use timers with do events.

  6. #6
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    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..

  7. #7
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Getting out of a running loop

    ehehehe.. sorry didn't read and check dee-u's posted code.

  8. #8

    Thread Starter
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    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::


  9. #9
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Getting out of a running loop

    yup.. dee-u did not initialize his global variable NoMore...


    VB Code:
    1. Option Explicit
    2. Dim DoTask As Boolean
    3.  
    4. Private sub Form_Load()
    5.     DoTask = true
    6. end sub
    7.  
    8. 'First Button button click event
    9. Private Sub Command1_Click()
    10.     'set the flag to do the task to false to stop doing the flag
    11.     DoTask = false
    12. End Sub
    13.  
    14. 'Second Button click event
    15. Private Sub Command2_Click()
    16.     'call loop here
    17.     call subDoLoop()
    18. End Sub
    19.  
    20. Private Sub subDoLoop()
    21.     Dim a As Double
    22.     For a = 0 To 100000000
    23.         If Not DoTask Then
    24.             MsgBox "Process Cancelled"
    25.             Exit For
    26.         End If
    27.  
    28.         'Do your processing here... :)
    29.  
    30.         ' Do events returns control to windows to allow processing of other windows commands one of which is possible the click button to pause... :)
    31.         DoEvents
    32.     Next
    33.     MsgBox "Finish"
    34. End Sub


    changed code to make it clearer.
    and added some comments.

  10. #10
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Getting out of a running loop

    Quote Originally Posted by oceanebelle
    yup.. dee-u did not initialize his global variable NoMore...


    VB Code:
    1. Option Explicit
    2. Dim DoTask As Boolean
    3.  
    4. Private sub Form_Load()
    5.     DoTask = true
    6. end sub
    7.  
    8. 'First Button button click event
    9. Private Sub Command1_Click()
    10.     'set the flag to do the task to false to stop doing the flag
    11.     DoTask = false
    12. End Sub
    13.  
    14. 'Second Button click event
    15. Private Sub Command2_Click()
    16.     'call loop here
    17.     call subDoLoop()
    18. End Sub
    19.  
    20. Private Sub subDoLoop()
    21.     Dim a As Double
    22.     For a = 0 To 100000000
    23.         If Not DoTask Then
    24.             MsgBox "Process Cancelled"
    25.             Exit For
    26.         End If
    27.  
    28.         'Do your processing here... :)
    29.  
    30.         ' Do events returns control to windows to allow processing of other windows commands one of which is possible the click button to pause... :)
    31.         DoEvents
    32.     Next
    33.     MsgBox "Finish"
    34. 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.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  11. #11
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    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?

  12. #12
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Getting out of a running loop

    Quote 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.

  13. #13
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Getting out of a running loop

    Have you declared GLOBAL variablies?

  14. #14
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Getting out of a running loop

    Quote Originally Posted by dglienna
    Have you declared GLOBAL variablies?

    sorry wrong term..


    global variable for the module... module variables??? isn't it the same?

  15. #15
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  16. #16
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Getting out of a running loop

    Quote 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?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  17. #17

    Thread Starter
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    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::


  18. #18
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Getting out of a running loop

    Quote Originally Posted by dee-u
    I'm intrigued... May I know that?

    me too

  19. #19
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Getting out of a running loop

    Quote 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...

  20. #20
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    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????

  21. #21
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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...
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  22. #22

    Thread Starter
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    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::


  23. #23
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Getting out of a running loop

    Quote 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?

  24. #24

    Thread Starter
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    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::


  25. #25
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Getting out of a running loop

    dee-u's solution will work.. even used it in mine. ...*sigh*

    oh well.. good luck

  26. #26

    Thread Starter
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    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::


  27. #27
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Getting out of a running loop

    This might work...

    VB Code:
    1. Option Explicit
    2. Dim NoMore As Boolean
    3.  
    4. Private Sub Command1_Click()
    5.     NoMore = True
    6. End Sub
    7.  
    8. Private Sub Command2_Click()
    9.     Test
    10. End Sub
    11.  
    12. Private Sub Test()
    13.     Dim Check As Boolean
    14.     For x = 0 To List1.ListCount - 1
    15.         For i = 0 To List2.ListCount - 1
    16.             DoEvents
    17.             If NoMore = True Then
    18.                 MsgBox "Stop"
    19.                 Exit Sub
    20.             End If
    21.             User.Caption = List2.List(i)
    22.             pass.Caption = List1.List(x)
    23.             Check = LoginCheck(List2.List(i), List1.List(x))
    24.             If Check = False Then
    25.                 Status.Caption = "Getting next user"
    26.             End If
    27.         Next i
    28.     Next x
    29. End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  28. #28

    Thread Starter
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    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::


  29. #29
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Getting out of a running loop[Resolved]

    Glad to help.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  30. #30
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Getting out of a running loop

    Quote 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
  •  



Click Here to Expand Forum to Full Width