VS 2010 [RESOLVED] Use controls while a loop is running?-VBForums
Results 1 to 8 of 8

Thread: [RESOLVED] Use controls while a loop is running?

  1. #1

    Thread Starter
    Member Shadow45o's Avatar
    Join Date
    Sep 2008
    Posts
    51

    Resolved [RESOLVED] Use controls while a loop is running?

    I did search around before posting, if I missed the solution to this problem, please post a link so that I may solve this as well.

    I'm hoping there is a simple answer to this, but I am unsure.

    Basically what I have are two loops, one inside the other, that will run until the given condition is met. Exactly what I want. My problem is that I want to be able to use the controls, like buttons, while a loop is going on.

    I originally did not think of this until I had built this part of my program, what I want to know is if there is a way to use controls while a loop is going on or if their is an alternative.

    Just some of my ideas
    1)I know timers can be used to meet these goals, but they did not work out as I had hoped and using two timers (1 to represent each loop) I do not believe is efficient.

    2)I have considered using recursion, but have found this may not be very efficient either and am not quite sure if this will do what I want.

    Some final notes
    These loops run as long as the number the user inputted. So it could theoretically run for 100,000+ times. I am not looking for speed, but it would be good if there were a faster method that allowed me to use controls while it loops.

    Thoughts or help is much appreciated.

    Thanks in advance.

    *NOTE* I did not believe this requires my code since this is more of a general question, if you need any more info or clarification, please let me know.

  2. #2
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,420

    Re: Use controls while a loop is running?

    There are two solutions, one is easy, the other more complicated.

    1) Application.DoEvents inside the loop.

    2) Move the loops into a BackGroundWorker component or into a different thread (same result, different techniques).

    What you need to be able to do is to have the process pumping the message queue while the loop is running. Application.DoEvents pauses the loop to let the application pump the message queue. That will work, but it has a bit of overhead. If the inner loop is really fast, then put this line in the outer loop such that the DoEvents is called once per iteration of the outer loop. If the inner loop is slow, then put the DoEvents inside the inner loop. For the app to be responsive, you want this method to be called at a frequency of once every half second, or so, which gives you an idea of where in the loops you want it. If it is only called every second, the controls will appear to lag. Longer than that and the app will appear frozen. I have successfully used this to keep controls responsive in a loop that ran for three days at a time.

    The second alternative is to shift the long running stuff into a different thread. This leaves the UI thread free to handle messages as they arise since it is no longer occupied with the loop. Threads are more difficult to set up, though only moderately so, but threads are also the preferred solution to this problem. For reasons that I don't really understand, people are a bit opposed to DoEvents. It does have a cost, so you don't want to be calling it a thousand times a second, but anything short of that works fine.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Member Shadow45o's Avatar
    Join Date
    Sep 2008
    Posts
    51

    Re: Use controls while a loop is running?

    Cool, the 'Application.DoEvents' seems to work.

    Thanks for the help

  4. #4
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,420

    Re: [RESOLVED] Use controls while a loop is running?

    The reason you didn't find it while searching is that you wouldn't unless you either knew to search on that term (in which case why search), or if you got lucky. It's not something that is recommended often, but it is ideal for the situation you described.
    My usual boring signature: Nothing

  5. #5
    Frenzied Member i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    1,846

    Re: [RESOLVED] Use controls while a loop is running?

    I know that this is RESOLVED ... but i wouldn't recommend do events... chews alot of cpu...

    use a thread and low its priority also i would sugguest sleeping 1ms between checking again in your loop to reduce cpu even more... also i would not suggest looping to check a condition unless it is absolutely necessary - if the condition can be picked up with events i would suggest that. You state that "or if their is an alternative" well we cannot specify if there is a better alternate unless we know the condition.

    Kris

  6. #6
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,420

    Re: [RESOLVED] Use controls while a loop is running?

    What i00 just stated got me thinking that I had misread the original question. After reading again, I'm only sure that one of us had, though not which one.

    The risk that i00 is pointing out is only true if what you are doing is busy waiting, which is a really bad thing to do, but it isn't at all clear whether that is what you are doing. All you stated, as far as I can find, is that you have loops "that will that will run until the given condition is met". This would be a fine description of what happens in a genetic algorithm. You loop through the perpetual cycle of reproduction until you reach some defined endpoint. That's the kind of situation where DoEvents is a good solution. However, I could also read that as having this:
    Code:
    Do While Not Condition
     Application.DoEvents
    Loop
    That would be a busy wait, which would be really bad, as i00 stated. The difference is critical. If there is a whole lot going on inside that loop, then application.DoEvents is fine. If there is NOTHING going on inside that loop, then you are polling for an event with a busy wait, which is terrible. Since you mentioned a loop within a loop, which I have never seen anyone do when implementing a busy wait, I think that you might be fine. Just avoid a busy wait!! That loop must be doing something useful in each iteration or else the DoEvents is the wrong answer.

    @i00: You might want to look at thread priority. From what I have read, you should almost never mess with that setting. All threads at a higher priority run before threads at a lower priority. That doesn't mean that threads at a lower priority are last in line for CPU time. What it means is that threads of lower priority get no CPU time at all if there is even one thread of higher priority running. Whether .NET implements threading in a different way where a low priority thread gets at least a little time when higher priority threads are available, I don't know, but that's how threading works overall.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Member Shadow45o's Avatar
    Join Date
    Sep 2008
    Posts
    51

    Re: [RESOLVED] Use controls while a loop is running?

    Ok, let me see if I can clarify some things here, sorry I wasn't entirely clear.

    Basically I have two text boxes that each take in number inputs. These numbers are then used to decide how long each loop will run. I have maxed one box so it can not be larger than 99 and the other so it can not be larger than 9999.

    The outer loop will be able to run the largest number (9999) while the inner runs only to the max of 99. I am using 'for loops' in order to do this.

    Example:
    Code:
    for i as integer = 0 to textbox1.text
      'do stuff
         for j as integer = 0 to textbox2.text
         'do stuff
         next
    next
    Basically, all I needed to use the controls for was a simple stop button. While the loop is running, all the controls are disabled aside from a ListView (which is having items being added to it) and a stop button that changes a boolean so that the loop will stop early.

    I am unsure how to use threads, but if that still may be a better option, could someone please point me to some good tutorial? Otherwise, I think threads may be un-needed since all I am doing is adding to a listview and just enabling a simple stop button.

    Thanks again for the help, hope this clears up some things here.

  8. #8
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,420

    Re: [RESOLVED] Use controls while a loop is running?

    No, you're fine. Your description matches some genetic algorithm code I have that uses DoEvents. It works great. The only concern would be if you didn't have those 'Do stuff lines in there. As long as you are doing stuff, you are all set. Threading is certainly something to consider, but only if the performance is not acceptable. In that case, there may be some advantage to threading if iterations of the loops can happen in parallel, which is something only you would know.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.