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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:28:14+00:00 2026-05-16T10:28:14+00:00

This question has been brought up many times, but I’d like to ask it

  • 0

This question has been brought up many times, but I’d like to ask it again because I’ve read some things here that didn’t seem right to me (could be because one is related to the .NET CF), and I am asking for validation of a specific approach that I think is reasonable, and would appreciate any feedback you might have.

Anyhow, the situation is much like everyone elses — I want to handle errors originating from a thread. Currently, the code I have works fine because I’m throwing exceptions from a thread, but I am using Invoke, rather than BeginInvoke.

You’re probably wondering at this point, “Why would he create a thread and then call Invoke? Why not just call a function?”. The issue is that I want to give the caller flexibility via a parameter that dictates whether or not the operation should be synchronous or asynchronous.

As a test, I have tested it in the asynchronous mode of operation, and as expected, it fails silently and the application dies in the debugger at the point the exception is thrown from the worker thread.

To make sure I have a basic understanding of the behavior of exceptions in threads, as well as the disprove the statement in this question on SO (at least as far as .NET 3.5 goes), I wrote some test code in a WPF app:

code removed

So it seems like there should be no problem handling events in the main thread that originate from another thread.

Provided that you agree with my tests above, my question is simply this — is it an acceptable practice to write my method that offers both sync and async behavior where all of the business logic is wrapped in a try/catch block? Internal errors get trapped inside of the thread, and if it’s a sync call, just throw the exception, and if it’s an async call, just raise the event? In my sample code above, I also throw the exception after raising the event. I’m not sure if or why this would cause me any problems.

Pseudo-pseudocode:

TestFunc(async);

private TestFunc(bool async)
{
  try {
    throw new MyAppException("error occurred.");
  } catch(MyAppException ex) {
    async ? RaiseErrorEvent : throw;
  }
}

UPDATE

Ok, as Hans said, it will cause issues no matter what if I throw the exception from the thread — period. I was able to test this case out and sure enough, the exception gets thrown and if you hit F5, it just gets thrown over and over again and never terminates the thread. I don’t understand this particular behavior — is it just how it works when running through the debugger? I guess this means that I have to check if the method is getting called sync or async. If sync, throw error. If async, raise event.

Perhaps a better way to do it is to force the client to always handle errors with events, rather than catching exceptions? Anyone have thoughts on this approach?

  • 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-16T10:28:14+00:00Added an answer on May 16, 2026 at 10:28 am

    If you’re talking about executing a task asynchronously, the most typical pattern is to catch the exception in thread and rethrow it once you eventually synchronize to wait for task completion. For instance, the IAsyncResult pattern has a BeginX(), EndX() call structure where you call EndX() to block until completion. The EndX() call should throw any exception that happened in the asynchronous operation so that the caller can decide how to handle it.

    If you’re using .NET 4.0, the Task Parallel Library has extensive support for capturing and even aggregating exceptions while performing asynchronous tasks so that the client that launched the tasks can decide how to handle them.

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

Sidebar

Related Questions

This question has been asked a lot of times in many forums. But I
This question has been asked many times before, but they all seem to relate
This question has been asked before perhaps multiple times, but I can't get the
This question has been asked before, but I would like a little more detail
This question has been already asked, but never answered. I want to write some
This question has been asked several times already, but has been solved with poor
This question has been asked on here a few times, but none of the
This question has been asked few times here and there, but you see all
This question has been asked a number of times, I have noted, but none
This question has been brought up before, and I have searched many of the

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.