C# WinForms。线程和递归
所以,我有一个最长路径搜索函数,它递归地遍历一些图。此功能必须在上停止,等待,直到单击表单中的“下一步”按钮。但我无法理解如何防止函数在等待后返回到以前的调用:C# WinForms。线程和递归,c#,multithreading,winforms,C#,Multithreading,Winforms,所以,我有一个最长路径搜索函数,它递归地遍历一些图。此功能必须在上停止,等待,直到单击表单中的“下一步”按钮。但我无法理解如何防止函数在等待后返回到以前的调用: public async void LongestPath(T current, T from, T to, Stack<Graph<T, W>.Edge> path, W summary) { if (EqualityComparer<T>.D
public async void LongestPath(T current, T from, T to, Stack<Graph<T, W>.Edge> path, W summary)
{
if (EqualityComparer<T>.Default.Equals(current, to)) //if arrived at the end node
{
if (Comparer<W>.Default.Compare(summary, longest) > 0) //if this path is the biggest
{
LongestExists = true;
longest = summary;
longestPath.Clear();
for (int i = 0; i < path.Count; i++)
longestPath.Push(path.ElementAt(i));
}
return;
}
List<Pair<T, Graph<T, W>.Edge>> available = new List<Pair<T, Graph<T, W>.Edge>>(); //current vertice's available routes
for (int i = 0; i < this.Edges.Count; i++)
{
Pair<T, Graph<T, W>.Edge> toAdd = new Pair<T, Graph<T, W>.Edge>();
if (!this.Edges[i].visited)
{
if (this.Edges[i].undirected)
{
if (EqualityComparer<T>.Default.Equals(this.Edges[i].Left, current) && !nodeVisited(this.Edges[i].Left))
{
toAdd.First = this.Edges[i].Right;
toAdd.Second = this.Edges[i];
available.Add(toAdd);
}
else if (EqualityComparer<T>.Default.Equals(this.Edges[i].Right, current) && !nodeVisited(this.Edges[i].Right))
{
toAdd.First = this.Edges[i].Left;
toAdd.Second = this.Edges[i];
available.Add(toAdd);
}
}
else if (EqualityComparer<T>.Default.Equals(this.Edges[i].Left, current) && !nodeVisited(this.Edges[i].Left))
{
toAdd.First = this.Edges[i].Right;
toAdd.Second = this.Edges[i];
available.Add(toAdd);
}
}
}
for (int i = 0; i < available.Count; i++)
{
available.ElementAt(i).Second.visited = true;
path.Push(available.ElementAt(i).Second);
visitNode(current, true); //function that just sets the "visited" flag of a vertice to true
Task check = new Task(() => Form1.CheckNextButtonAsync()); //this task checks the state of "Next" button
check.Start();
await check; //the problem
Form1.NextButtonClicked = false; //function continues from here after the button is clicked but at that moment it had already went back to previous calls of itself
Console.Beep();
LongestPath(available.ElementAt(i).First, from, to, path, GenericSum<W>(summary, available.ElementAt(i).Second.weight));
visitNode(current, false);
available.ElementAt(i).Second.visited = false;
path.Pop();
}
return;
}
public async void LongestPath(T current,T from,T to,Stack path,W summary)
{
if(EqualityComparer.Default.Equals(current,to))//如果到达结束节点
{
if(Comparer.Default.Compare(summary,longest)>0)//如果此路径最大
{
长期存在=正确;
最长=摘要;
最长路径。清除();
for(int i=0;iForm1.CheckNextButtonSync());//此任务检查“下一步”按钮的状态
check.Start();
等待检查;//问题已解决
Form1.NextButtonClicked=false;//单击按钮后,函数将从此处继续,但此时它已返回到以前的调用
Console.Beep();
LongestPath(可用.ElementAt(i).第一,from,to,path,GenericSum(摘要,可用.ElementAt(i).第二,weight));
visitNode(当前,错误);
可用。ElementAt(i).Second.visted=false;
path.Pop();
}
返回;
}
很明显,非递归函数将完美地工作。但是有没有办法“冻结”线程并使窗体保持工作状态,以便同时按下按钮?明智的做法是。@TheodorZoulias,所使用的任何方法都不会返回Task或Task。因此,如果我的想法正确,那么避免异步voidVoid是不明智的。返回异步方法有一个特定的目的:使异步事件处理程序成为可能。(引用我先前发布的链接)。是Microsoft建议您避免异步void,而不是我。如果您处理的是异步事件处理程序,那么async void就可以了。否则就不是了。