title: WinForms. Threading and recursion
body: <p>So, i have a longest path search function that recursively traverses through some graph. This function must stop on <strong>await</strong> until the "Next" button in form is clicked. But i cannot understand how to prevent function from going back to previous calls of itself after <strong>await</strong>:</p>
  
  <pre><code>        public async void LongestPath(T current, T from, T to, Stack&lt;Graph&lt;T, W&gt;.Edge&gt; path, W summary)
          {
              if (EqualityComparer&lt;T&gt;.Default.Equals(current, to)) //if arrived at the end node
              {
                  if (Comparer&lt;W&gt;.Default.Compare(summary, longest) &gt; 0) //if this path is the biggest
                  {
                      LongestExists = true;
                      longest = summary;
                      longestPath.Clear();
                      for (int i = 0; i &lt; path.Count; i++)
                          longestPath.Push(path.ElementAt(i));
                  }
                  return;
              }
              List&lt;Pair&lt;T, Graph&lt;T, W&gt;.Edge&gt;&gt; available = new List&lt;Pair&lt;T, Graph&lt;T, W&gt;.Edge&gt;&gt;(); //current vertice's available routes
              for (int i = 0; i &lt; this.Edges.Count; i++)
              {
                  Pair&lt;T, Graph&lt;T, W&gt;.Edge&gt; toAdd = new Pair&lt;T, Graph&lt;T, W&gt;.Edge&gt;();
                  if (!this.Edges[i].visited)
                  {
                      if (this.Edges[i].undirected)
                      {
                          if (EqualityComparer&lt;T&gt;.Default.Equals(this.Edges[i].Left, current) &amp;&amp; !nodeVisited(this.Edges[i].Left))
                          {
                              toAdd.First = this.Edges[i].Right;
                              toAdd.Second = this.Edges[i];
                              available.Add(toAdd);
                          }
                          else if (EqualityComparer&lt;T&gt;.Default.Equals(this.Edges[i].Right, current) &amp;&amp; !nodeVisited(this.Edges[i].Right))
                          {
                              toAdd.First = this.Edges[i].Left;
                              toAdd.Second = this.Edges[i];
                              available.Add(toAdd);
                          }
                      }
                      else if (EqualityComparer&lt;T&gt;.Default.Equals(this.Edges[i].Left, current) &amp;&amp; !nodeVisited(this.Edges[i].Left))
                      {
                          toAdd.First = this.Edges[i].Right;
                          toAdd.Second = this.Edges[i];
                          available.Add(toAdd);
                      }
                  }
              }
              for (int i = 0; i &lt; 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(() =&gt; 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&lt;W&gt;(summary, available.ElementAt(i).Second.weight));
                  visitNode(current, false);
                  available.ElementAt(i).Second.visited = false;
                  path.Pop();
              }
              return;
          }
  </code></pre>
  
  <p>It is pretty obvious that non-recursive function will work perfectly. But is there any way of "freeze" the thread and keep the form working so that the button could be pressed at the same time?</p>