I would recommend separating out your code a bit, rather than having a loop that "waits".
For example:
private int _loopIndexer = 0;
public void ContinueLoop()
{
for (; _loopIndexer < list.Count; ++_loopIndexer)
{
if (list[_loopIndexer].Property == "value")
{
// on a matching condition do something and then exit the method
++_loopIndexer; // we need to increment the position before we exit the method
return;
}
}
}
private void btnStarten_Click(object sender, RoutedEventArgs e)
{
// reset to the beginning
_loopIndexer = 0;
ContinueLoop();
}
private void btnContinue_Click(object sender, RoutedEventArgs e)
{
// If the end of the loop is reached, quit (or do whatever)
if (_loopIndexer + 1 == list.Count)
{
// _loopIndexer = 0; // you could reset it to 0 if you wanted to loop again
return;
}
ContinueLoop();
}
The idea is that btnStarten will start the loop, and the loop will stop whenever a condition is met. Then, when you click the Continue button, it will continue the loop if there are any list items left.
Try it online
forandforeachboth block the UI in the code you've provided. You should probably separate your code. Storeisomewhere else to keep track of where you are, and then have the button clicks trigger iteration.