Results 1 to 2 of 2

Thread: break out of a for loop?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2005
    Posts
    150

    break out of a for loop?

    Hi,
    I am writing a performance testing tool that will call an add document method of a webservice 1000 times. (the call to the method is just inside a for loop. for(int i=0; i<1000; i++)

    if the user decides after 500 method calls that they want the process to stop (by clicking a stop button) but doesn't want to exit the whole application, what code would need to go into the stop button to cause the previous action to exit the for loop?


    My idea in pseduo code is shown below, Not sure if this is the best way of doing it though???

    thanks in advance

    -------------------------
    global flag=true;

    while(flag==true)
    {
    for(int i=0; i<1000; i++)
    {
    //call web service method
    }
    break; //so that if the for loop finishes with the stop button being clicked it will not be executed again
    }


    stop button onclick handler
    {
    flag=false;
    }
    --------------------------------
    Last edited by eakin; Jan 9th, 2007 at 04:51 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: break out of a for loop?

    You don't need a while loop and a for loop. Use one or the other. Either increment an integer manually in a while loop or else test the flag manually inside a for loop. If you use the for loop and the flag tests false then execute a break statement.

    Having said that, as long as your loop is executing your Button's Click event handler will not be executed because the UI thread can only do one thing at a time. Either you would need to call Application.DoEvents inside the loop, which is inefficient, or else execute your loop in a worker thread so that the UI thread is free to handle events.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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