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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:21:54+00:00 2026-05-17T20:21:54+00:00

I have experienced an odd behavior of the BackgroundWorker or the .NET framework when

  • 0

I have experienced an odd behavior of the BackgroundWorker or the .NET framework when rethrowing an Exception in the RunWorkerCompleted event.

Exceptions occuring in the background thread are passed to RunWorkerCompleted. There I do a object temp = e.Result to rethrow the exception. Since this is supposed to happen in the main UI thread, I expect the exception to be propagated up to Application.Run(...), which I surrounded with a try-catch block.

When running the application in Visual Studio, this works fine. However, when I execute the exe file outside Visual Studio, the Exception is not handled and makes the application crash.

Could it be that the RunWorkerCompleted event is not executed in the main UI thread?

These posts are not duplicates but rather confirm that my situation is weird:

  • C# BackgroundWorker RunWorkerCompleted Event
  • Events raised by BackgroundWorker not executed on expected thread

This is an extremly simplified code example that reproduces the bug: Create a WinForms project and add:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    try
    {
        Form form = new Form();

        form.Load += delegate
        {
            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += (sender, e) => 
            {
                // do nothing
            };
            bw.RunWorkerCompleted += (sender, e) =>
            {
                throw new Exception("Booo!");
            };
            bw.RunWorkerAsync();
        };

        Application.Run(form);
    }
    catch (Exception ex)
    {
        while (ex is TargetInvocationException && ex.InnerException != null)
            ex = ex.InnerException;

        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

Then try:

  1. Run it in Visual Studio. You’ll get a friendly message box before the application quits.
  2. Run the compiled executable outside Visual Studio. You’ll get a .NET framework “unhandled exception” message.

The interesting part is that if I hit “continue” in 2, the application persists, with the Form being displayed and taking user input. This must mean that instead of the main UI thread, a background thread crashed. The main thread remains alive.

(Why do I want to do all these things? I display a splash screen, and while some application startup work is done in background, the splash screen displays the progress by means of a label and a progress bar. So I have to create and display the splash screen and then grab the main UI thread to start the BackgroundWorker. In the original code it reports progress back to the main thread which updates the splash form. If during startup an exception occurs, I want to catch it. In certain cases they are “business exceptions” which have a specific meaning, e.g. “you are not authorized to use this application”. In these cases I display a friendly message box before I let the application die. Further I have a finally block to clean up resources. I’m not sure whether the .NET framework “unhandled exception” dialog executes the finally block before killing the app.)

  • 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-17T20:21:54+00:00Added an answer on May 17, 2026 at 8:21 pm

    There is a try/catch block around the message loop code inside Application.Run() that catches unhandled exceptions raised by an event handler. The catch clause raises the Application.ThreadException event. There is a default handler for that event, it displays a ThreadExceptionDialog. Giving the user the option to ignore the error or abort the program.

    This catch clause is disabled when you run your program with a debugger. This allows you to easily debug exceptions. With it disabled, the CLR will find the catch clause in your Main() method. To disable this behavior, add this line of code to the top of your Main() method:

      Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
    

    It will now behave the same way as in the debugger. The better mousetrap here is to implement an event handler for AppDomain.CurrentDomain.UnhandledException. This catches all unhandled exceptions, including those raised in worker threads. And lets you debug unhandled exceptions, the debugger stops at the throw statement.

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

Sidebar

Related Questions

I've experienced rather strange behavior of JSTL forEach tag. I have some bean called
I know this isn't strictly a programming question but y'all must have experienced this.
Most of experienced programmer knows data alignment is important for program's performance. I have
First of all: I am not an experienced ClearCase user, but I have lots
On the advice of a more experienced developer, I have always coded my web
I have an ASP.Net 3.5 website running in IIS 6 on Windows Server 2003
I've seen several classes in the framework with this behavior (a refresh symbol in
Anyone have experience with algorithmic trading (like stocks)? Any good services to use to
Anyone have experience with adding a ReSharper profile to the VS2008 solution and share
I have experience in PHP, JSP/servelts and classic ASP, and I am trying to

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.