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 560759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:22:40+00:00 2026-05-13T12:22:40+00:00

I have a multi-thread C# application that uses some recursive functions in a dll.

  • 0

I have a multi-thread C# application that uses some recursive functions in a dll. The problem that I have is how to cleanly stop the recursive functions.

The recursive functions are used to traverse our SCADA system’s hierarchical ‘SCADA Object’ data. Traversing the data takes a long time (10s of minutes) depending on the size of our system and what we need to do with the data.

When I start the work I create a background thread so the GUI stays responsive. Then the background worker handles the calling of the recursive function in the dll.

I can send a cancel request to the background worker using CancelAsync but the background worker can’t check the CancellationPending flag because it is blocked waiting of the dll’s recursive function to finish.

Typically there is only 1 recursive function active at a time but there are dozens of recursive functions that are used at various times by different background workers.

As a quick (and really shameful) hack I added a global ‘CodeEnabled’ flag to the dll. So when the GUI does the CancelAsync it also sets the ‘CodeEnabled’ flag to false. (I know I need some of those bad code offsets). Then the dll’s recursive loop checks the ‘CodeEnabled’ flag and returns to the background worker which is finally able to stop.

I don’t want to move the recursive logic to the background worker thread because I need it in other places (e.g. other background workers).

What other approach should be used for this type of problem?

  • 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-13T12:22:40+00:00Added an answer on May 13, 2026 at 12:22 pm

    It depends on the design, really. Much recursion can be replaced with (for example) a local stack (Stack<>) or queue (Queue<>), in which case a cancel flag can be held locally without too much pain. Another option is to use some kind of progress event that allows subscribers to set a cancel flag. A third option is to pass some kind of context class into the function(s), with a (volatile or synchronized) flag that can be set.

    In any of these cases you should have relatively easy access to a cancel flag to exit the recursion.

    FooContext ctx = new FooFontext();
    BeginSomeRecursiveFunction(ctx);
    ...
    ctx.Cancel = true; // or ctx.Cancel(), whatever
    

    with (in your function that accepts the context):

    if(ctx.Cancel) return; // or maybe throw something
                           // like an OperationCancelledException();
    blah...
    CallMyself(ctx); // and further down the rabbit hole we go...
    

    Another interesting option is to use iterator blocks for your long function rather than regular code; then your calling code can simply stop iterating when it has had enough.

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

Sidebar

Ask A Question

Stats

  • Questions 313k
  • Answers 313k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First convert your DSLR file to DRL, then do the… May 13, 2026 at 10:54 pm
  • Editorial Team
    Editorial Team added an answer You probably set MyObject as a resource in routes.rb. Resources… May 13, 2026 at 10:54 pm
  • Editorial Team
    Editorial Team added an answer It's a quadratic function. If t is the time passed,… May 13, 2026 at 10:54 pm

Related Questions

I've got an application, written in C++, that uses boost::asio. It listens for requests
I have an intermittent problem with some code that writes to a Windows Event
I have created a .Net application to run on an App Server that gets
I have a server application that receives information over a network and processes it.
I'm running a C# Console Application that is multi-threaded. The core process retrieves some

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.