|
-
Jan 9th, 2007, 04:32 PM
#1
Thread Starter
Addicted Member
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.
-
Jan 9th, 2007, 05:32 PM
#2
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.
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
|