Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 698749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:18:37+00:00 2026-05-14T03:18:37+00:00

I have a problem in choosing the right method to accomplish my goal. I’m

  • 0

I have a problem in choosing the right method to accomplish my goal.
I’m working on Algorithms teaching system, I’m using C#. I need to divide my algorithm into steps, each step will contain a recursion.
I have to stop execution after each step, user can then move to the next step(next recursion) using a button in my GUI.

After searching, threads was the right choice, but I found several methods:

  • (Thread.sleep/interrupt): didn’t work, my GUI freezed !!

  • (Suspend/Resume): I’ve read that it’s a bad idea to use.

  • (Waithandles): still reading about them.

  • (Monitor wait/resume).

I don’t have much time to try and read all previous methods, please help me in choosing the best method that fits my system.Any suggestions are extremal welcomed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:18:37+00:00Added an answer on May 14, 2026 at 3:18 am

    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.”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem using the Java search function in Eclipse on a particular
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with return statment >.< I want to store all magazine names
I have problem with starting processes in impersonated context in ASP.NET 2.0. I am
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem with ActionLink. I'd like to pass to my ActionLink parameter for
I have problem when I try insert some data to Informix TEXT column via
I do not have problem as such but I am quite new to Ruby.
I have a problem with a little .Net web application which uses the Amazon
I have a problem in my project with the .designer which as everyone know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.