c# - 如何在不阻塞 UI 的情况下暂停循环并使用按钮重新启动
问题描述
我正在寻找暂停foreach
或for
循环的最佳做法(它必须等到用户插入文本并按下按钮确认)。
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
}
}
解决方案
我的最终代码如下所示:
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));
}
推荐阅读
- java - 将带有数据的 HTML 导出到 MS Word
- php - createfromformat 在 foreach 循环中不起作用
- html - Django:忽略ManytoManyField的只读表单控件
- java - 如何在 Java 客户端中禁用 Gemfire DEFAULT 池创建
- powershell - Get-DnsServerResourceRecord 在 2016 服务器上不起作用
- python - Ray 究竟是如何与工人共享数据的?
- c - 使用 GCC asm 时 MUL 出现运行时错误
- excel - 下标超出范围(错误 9) - Windows(name).Activate
- go - Windows 上的 VIM-Go 调试器导致“找不到 Goroutine”
- c++ - 从指针调用方法时出现分段错误