gpt4 book ai didi

C# 暂停 foreach 循环,直到按下按钮

转载 作者:行者123 更新时间:2023-11-30 21:55:17 30 4
gpt4 key购买 nike

使用 C#,我有方法列表(操作)。然后我有一个使用 foreach 循环调用操作的方法。单击按钮会调用该方法,该方法又会一次性调用列表中的每个操作。我所追求的是每次点击仅执行一个操作。提前致谢。

private static List<Action> listOfMethods= new List<Action>();

listOfMethods.Add(() => method1());
listOfMethods.Add(() => method2());
listOfMethods.Add(() => method3());
//====================================================================
private void invokeActions()
{
foreach (Action step in listOfMethods)
{
step.Invoke();
//I want a break here, only to continue the next time the button is clicked
}
}
//====================================================================
private void buttonTest_Click(object sender, EventArgs e)
{
invokeActions();
}

最佳答案

您可以添加计步器:

private static List<Action> listOfMethods= new List<Action>();
private static int stepCounter = 0;

listOfMethods.Add(() => method1());
listOfMethods.Add(() => method2());
listOfMethods.Add(() => method3());
//====================================================================
private void invokeActions()
{
listOfMethods[stepCounter]();

stepCounter += 1;
if (stepCounter >= listOfMethods.Count) stepCounter = 0;
}
//====================================================================
private void buttonTest_Click(object sender, EventArgs e)
{
invokeActions();
}

关于C# 暂停 foreach 循环,直到按下按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32383629/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com