首页 > 解决方案 > 如何在不阻塞 UI 的情况下暂停循环并使用按钮重新启动

问题描述

我正在寻找暂停foreachfor循环的最佳做法(它必须等到用户插入文本并按下按钮确认)。

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
      }   
}

标签: c#for-loopevents

解决方案


我的最终代码如下所示:

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));
        }



推荐阅读