0

I am looking for the best practice to pause foreach or for loops (it has to wait until the user inserts text and presses a button to confirm).

private void btnStarten_Click(object sender, RoutedEventArgs e)
{            
      //using a foreach loop blocks the UI I read? 

      for (int i = list.Count; i >= 0; i--)
      {
          //Here I want to pauze the loop and being able to insert a valeu in a textbox after I 
          //clicked another button to confirm, I resume the Loop
      }   
}
1
  • for and foreach both block the UI in the code you've provided. You should probably separate your code. Store i somewhere else to keep track of where you are, and then have the button clicks trigger iteration. Commented Apr 17, 2020 at 8:45

2 Answers 2

1

My final code looked like this:

private void btnResultaat_Click(object sender, RoutedEventArgs e)
        {
            if (leerlingIngave.RandomLijst.Count - 1 == leerlingIngave.AntwoordEnUitkomstLijst.Count)
//I compair my 2 lists to eachother (theRandom list was already filled)
            {

                btnResultaat.IsEnabled = false; // I ended the loop by disabeling the button
                leerlingIngave.VerderZettenLoop();
            }
            else
            {
                leerlingIngave.VerderZettenLoop();
            }
        }

public void VerderZettenLoop() // Add object to the list
        {

                    AntwoordEnUitkomstLijst.Add(new LeerlingModel(Uitkomst, Antwoord));
        }


Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.