Fundamentally, this question is not really about recursion or multi-threading, it is simply this:
How do I execute a long-running operation in the background of a GUI app so that the app stays responsive?
Implementing your own threading model is not the way to go here, especially if you are just starting to learn about multi-threaded/async operations. The .NET Framework already has a component for what you want to do: BackgroundWorker, which works in both Winforms and WPF (and almost any other architecture).
It is very, very easy to do what you want using the BackgroundWorker
. I'll assume Winforms for this example but this is just as easy in WPF.
// Don't actually write this line; it will be in the .designer.cs file when you
// drop a BackgroundWorker onto the form/control. This is for reference only.
private BackgroundWorker bwRecursive;
private void bwRecursive_DoWork(object sender, DoWorkEventArgs e)
{
MyTreeNode root = (MyTreeNode)e.Argument;
ExecuteRecursiveOperation(root);
}
private void bwRecursive_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
// Optionally update the GUI with the results here
}
private void ExecuteRecursiveOperation(MyTreeNode node)
{
if (bwRecursive.CancellationPending)
return;
foreach (MyTreeNode childNode in node.ChildNodes)
{
if (bwRecursive.CancellationPending)
break;
ExecuteRecursiveOperation(childNode);
}
}
You obviously also have to wire up the DoWork
and RunWorkerCompleted
events, and make sure to set WorkerSupportsCancellation
to true
on the BackgroundWorker
. After that, you run the operation with:
bwRecursive.RunWorkerAsync(someTreeNode);
And cancel with:
bwRecursive.CancelAsync();
The only wrinkle here is that you say you want to pause (not stop) execution after each "step". I would probably do this using an AutoResetEvent
, which is a type of event that resets its signaled ("ready") state every time a wait succeeds. Again, it's only a few lines of code to integrate:
public class MyForm : Form
{
private AutoResetEvent continueEvent = new AutoResetEvent(false);
// Previous BackgroundWorker code would go here
private void ExecuteRecursiveOperation(MyTreeNode node)
{
if (bwRecursive.CancellationPending)
return;
foreach (MyTreeNode childNode in node.ChildNodes)
{
continueEvent.WaitOne(); // <--- This is the new code
if (bwRecursive.CancellationPending)
break;
ExecuteRecursiveOperation(childNode);
}
}
private void btnContinue_Click(object sender, EventArgs e)
{
continueEvent.Set();
}
private void btnCancel_Click(object sender, EventArgs e)
{
bwRecursive.CancelAsync();
continueEvent.Set();
}
private void btnStart_Click(object sender, EventArgs e)
{
continueEvent.Set();
bwRecursive.RunWorkerAsync(...);
}
}
There's one thing that might warrant additional explanation here and that is the cancel method, which first cancels and then sets the continueEvent
. It's necessary to do this because if the worker is still waiting for the event, it won't actually get to the cancellation stage, so when you cancel, you need to allow the worker to continue. You also need to set the continueEvent
when you start the worker if you want it to execute the first step without requiring the user to hit "continue."