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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:15:49+00:00 2026-05-15T21:15:49+00:00

This is a follow-on from: Debugging a Multithreaded C# – C++/CLI – C++ Solution

  • 0

This is a follow-on from:

Debugging a Multithreaded C# – C++/CLI – C++ Solution in Visual Studio 2008: What are these threads?

Please excuse the format, I’ve just repeated some of the description of the application here:

I’ve inherited a project consisting of three levels of code. The lowest layer is native C++ that interacts with hardware. This is mature, stable and well-tested. The intermediate level code is C++/CLI, which interacts with top-level C# code that contains the UI elements and some additional functionality. This C# code is incomplete and was rushed in development: it crashes frequently and is not fit for purpose. My task is debug it and complete it.

I received some very helpful info from the last question I asked – but now there’s more issues! My problem at the moment is that when I invoke Application.Exit() to shut down the UI and quit the application, an exception is thrown:

System.InvalidOperationException: Collection was modified; enumeration operation may not execute

I understand that this is because I need to ensure that all of my threads are ended before I call Application.Exit() (or Application.ExitThread()). I’ve tried using MainForm.Close() as quick fix while I investigate further but it doesn’t alleviate the problem.

I don’t want to just called Thread.CurrentThread.Abort(), mainly because some of the threads originate in the C++ section of the code, via Boost::Thread, and I’m unsure exactly what resources I may leave in an undesirable state (a lot of the code consists of objects for interaction with hardware, such as a serial port – it’s not been implemented via RAII so I’m rather cautious of brute forcing things).

What I’d like to be able to do is identify what threads are doing what, and gracefully end them prior to exiting the application. However, in VS 2008, the stack trace – with ‘Show External Code’ activated – only reveals

[Native to managed transition]
[Managed to native transition]

so I’m still having difficulty tracing the individual native threads and thus working out the best way to end them.

I tried using Allinea DDTLite, which seemed excellent – but I’ve had some issues with my VS installation and I had to disable the plug-ins, so it wasn’t a solution.

To summarise: what is the most effective way to ensure that all threads – both managed and native – are correctly finished so that the UI and then the
the entire application can exit cleanly?

  • 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-15T21:15:50+00:00Added an answer on May 15, 2026 at 9:15 pm

    What I’d like to be able to do is identify what threads are doing what

    You cannot make this work. There are ways to enumerate the threads in a process, like the managed Process.Threads property or the native Thread32First/Next but you don’t get nearly enough info about the threads to know what they do. And most certainly not to shut them down cleanly. Further complicated by the .NET framework using threads for its own purposes, like the debugger thread and the finalizer thread and a smattering of threadpool threads.

    You can kill these threads rudely with TerminateThread, albeit that killing the finalizer thread will crash the program immediately, but that’s no different from rudely terminating the process with Environment.Exit(). With the caveat that nothing is cleaned-up nicely. Windows will clean up most of the shrapnel though.

    This should not normally be a problem. You know what threads you started, there should also be a mechanism to ask them to shut down. That’s normally done by signaling an event, something that’s tested in thread’s main loop. Waiting on the thread handle confirms that the thread indeed exited. After which you can close the windows.

    But that’s probably plumbing that’s missing, you’ll have to add it. If the current native C++ code has no mechanism to take care of thread shutdown then you’ve got a fairly big problem. I’ll guess that maintaining this native C++ code is the real problem. You may have to hire a gun to get this done.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use array_slice: $output = implode(';', array_slice($array, 0, 3)); May 16, 2026 at 12:16 pm
  • Editorial Team
    Editorial Team added an answer After hours of pulling my hair out I found the… May 16, 2026 at 12:16 pm
  • Editorial Team
    Editorial Team added an answer Assuming there will not be any whitespace within the "word"… May 16, 2026 at 12:16 pm

Trending Tags

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

Top Members

Related Questions

This is a follow on from my previous question although this is about something
This is a follow up to this question , where I didn't get any
This is a follow up to my previous question: Problem passing parameters via Iframe
As a follow-up to my recent question about .NET Compact Framework debugging, I am
This is continuing questions from Selecting the highest salary Assuming a table ' wagetable
This is a follow up to a previous question . if I have multiple
I try many different solution on this site and none seems to work for
Coming from Java and Python, I am not so well versed in memory management,
I am looking for a way to dump input into my terminal from a
I have application reads in data from text file. Recently I realized that I

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.