此代码是算法搜索,目标是当表从1到16排序时(在游戏表的第一个中没有订购)。我想在填充UI表(在UI中显示游戏结果的表)时有延迟,因为我想向玩家展示通过算法解决游戏的过程。
如何在//Make Delay
中延迟(1或2秒)?
public bool BFS_TreeSearch(int[,] table, Queue<int[,]> fringe)
{
MakeNode(table, fringe);
do
{
if (fringe.Count == 0)
return false;
int[,] node = fringe.Dequeue();
//Make Delay
FillTableUI(node);
if (isGoal(node))
return true;
MakeNode(node, fringe);
} while (true);
}
答案 0 :(得分:1)
您可以根据自己的要求使用Thread.Sleep
或Task.Delay
来使用同步或异步睡眠。由于你想在UI上展示一些东西,我认为你应该像这样使用后者。请注意,这会强制您制作方法async
,因此您必须以不同方式调用它。
public async Task<bool> BFS_TreeSearch(int[,] table, Queue<int[,]> fringe)
{
MakeNode(table, fringe);
do
{
if (fringe.Count == 0)
return false;
int[,] node = fringe.Dequeue();
await Task.Delay(2000) // continue from here after 2 seconds
FillTableUI(node);
if (isGoal(node))
return true;
MakeNode(node, fringe);
} while (true);
}
请参阅本主题中的this post。