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

  • Home
  • SEARCH
  • 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 45789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:51:52+00:00 2026-05-10T15:51:52+00:00

The code that I want to write is like this: void MethodOnThreadA() { for

  • 0

The code that I want to write is like this:

void MethodOnThreadA() {     for (;;)     {         // Do stuff         if (ErrorConditionMet)             ThrowOnThread(threadB, new MyException(...));     } }  void MethodOnThreadB() {     try     {         for (;;)         {             // Do stuff         }     }     catch (MyException ex)     {         // Do the right thing for this exception.     } } 

I know I can have thread B periodically check, in thread safe way, to see if a flag has been set by thread A, but that makes the code more complicated. Is there a better mechanism that I can use?

Here is a more fleshed out example of periodically checking:

Dictionary<Thread, Exception> exceptionDictionary = new Dictionary<Thread, Exception>();  void ThrowOnThread(Thread thread, Exception ex) {     // the exception passed in is going to be handed off to another thread,     // so it needs to be thread safe.     lock (exceptionDictionary)     {         exceptionDictionary[thread] = ex;     } }  void ExceptionCheck() {     lock (exceptionDictionary)     {         Exception ex;         if (exceptionDictionary.TryGetValue(Thread.CurrentThread, out ex))             throw ex;     } }  void MethodOnThreadA() {     for (;;)     {         // Do stuff         if (ErrorConditionMet)             ThrowOnThread(threadB, new MyException(...));     } }  void MethodOnThreadB() {     try     {         for (;;)         {             // Do stuff             ExceptionCheck();         }     }     catch (MyException ex)     {         // Do the right thing for this exception.     } } 
  • 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. 2026-05-10T15:51:52+00:00Added an answer on May 10, 2026 at 3:51 pm

    This is NOT a good idea

    This article talks about ruby’s timeout library. which throws exceptions across threads.

    It explains how doing such a thing is fundamentally broken. It’s not just broken in ruby, it’s broken anywhere that throws exceptions across threads.

    In a nutshell, what can (and does) happen is this:

    ThreadA:

    At some random time, throw an exception on thread B: 

    ThreadB:

    try {     //do stuff } finally {     CloseResourceOne();     // ThreadA's exception gets thrown NOW, in the middle      // of our finally block and resource two NEVER gets closed.     // Obviously this is BAD, and the only way to stop is to NOT throw     // exceptions across threads     CloseResourceTwo(); } 

    Your ‘periodic checking’ example is fine, as you’re not actually throwing exceptions across threads.
    You’re just setting a flag which says ‘throw an exception the next time you look at this flag’, which is fine as it doesn’t suffer from the ‘can be thrown in the middle of your catch or finally block’ problem.
    However, if you’re going to do that, you may as well just be setting an ‘exitnow’ flag, and using that and save yourself the hassle of creating the exception object. A volatile bool will work just fine for that.

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

Sidebar

Ask A Question

Stats

  • Questions 102k
  • Answers 102k
  • 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 Being well-formed XML and valid XHTML are two different issues.… May 11, 2026 at 8:15 pm
  • Editorial Team
    Editorial Team added an answer I compiled the following code: struct my_data { int x;… May 11, 2026 at 8:15 pm
  • Editorial Team
    Editorial Team added an answer Him Manzoor, Your question is a little open ended, and… May 11, 2026 at 8:15 pm

Related Questions

So, I'm trying to stub a database connector in order to write tests for
I have an OLE COM object that trying to write a wrapper for, I
Some background: I have an database that I want to use linq-to-sql to update
I have some existing code that retrieves data from a database using ADO.NET that

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.