开发者

C# breaking out of a loop from separate class/method

开发者 https://www.devze.com 2023-04-10 08:44 出处:网络
I have a block of code that repeats using a \"for\" loop, and each loop constructs a form to display some text. Some thing like the shorthand code below.

I have a block of code that repeats using a "for" loop, and each loop constructs a form to display some text. Some thing like the shorthand code below.

Main()
{

For (int x: x<=20; x++)
{

createform(string[x]);
}

}

So for each loop a different string is passed to a method that will construct a form as below.

createform void (string input_)

{

...
code to build form and add a开发者_如何学C button "cancelbutton"
form.text = intput_
.... 
form.cancelbutton.Click += // and I want this to cause the original loop to end.... 

}

No I know I could use the button to make int x greater than 20 and that would end the loop, but I don't actually know what the max value will be as this is dynamic. Again I could work this out and do he same thing but it seems a bit "messy".

Is there a neater way to cause the button click to exit the loop. How about if the Createform method is in a separate class to main, does that make any difference?


If your loop runs from 0 to 20 (or even over 9000) the user won't be able to click the cancel button in time. In fact, since it is all on one thread, the loop will finish before the UI responds to the click, but maybe I have misunderstood. Could you just have a boolean flag which you check each time you enter the loop and set it to false once the user clicks the button?


Just add a variable and code the click event:

static bool clicked;

Main()
{
clicked = false;
For (int x: (x<=20) && !clicked; x++)
{

createform(string[x]);
}

}

public static void Click_Detector(object sender, EventArgs e) {
  clicked = true;
}

Your routine would need to do something like this:

createform void (string input_)
{

...
code to build form and add a button "cancelbutton"
form.text = intput_
.... 
form.cancelbutton.Click += MainClass.Click_Detector;

}


You shouldn't be using a loop to create each form. Assuming you have a submit button. Every time the user clicks submit, you should explicitly show the next form. That way, if a user clicks cancel, you don't have to worry about the rest of the forms.


The first solution that comes to my mind is: return value of the method True/False and in foreach check for its return value, if False=> break.


Your psuedo code isn't indicating how you are showing the form in CreateForm(). Is this going to be a modal or non modal? It would have to be modal, otherwise, you'd just keep creating forms until the cows come home. Remember, adding a handler to the click button doesn't actually execute the handler method until the click button is clicked.

Presumably your click event handler could set the value of x to be greater than 20, but since you said you're not sure what the maximum is, you could have a do loop governed by a boolean flag, and your createform() could set the state of the flag.

I'd recommend rethinking your problem space, as this overall approach seems really convoluted. I don't understand your UI, but it seems like your intent is to have the user control the exit of an infinite loop, like those old RPGs that ask you "Do you want to go on an adventure Yes/No", and it would continue asking you until you hit "Yes". These are pointless and confusing.


Add a boolean to the class and have the click event set the boolean value. Then you can break from the loop. However, if your real world use is the question above I would agree with the answer given by mikerobi.


One good solution would be to use the CancelAsync() method of the BackgroundWorker class: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.cancelasync.aspx

If you don't want to work with a separate thread to improve the performance, that I don't recommend, is to set a class level boolean value when the button clicked, then the for loop checks that boolean value in each next process.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号