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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:11:43+00:00 2026-05-16T11:11:43+00:00

I noticed a few strange behaviors in a Windows Forms scenario which involves threads

  • 0

I noticed a few strange behaviors in a Windows Forms scenario which involves threads and UI, so, naturally, this means making use of the InvokeRequired property. The situation: my application makes use of a thread to do some work and the thread sends an event into the UI. The UI displays a message based on an Internationalization system which consists of a dictionary with keys. The I18N system cannot find a key in the dictionary and crashes.

Notes: application is in Debug Mode and I have a try-catch over the entire “Application.Run();” back in Program.cs. However, that try-catch is not reached, as what I will discuss here is based on inner Exception handling, but I mentioned it just in case.

So now here comes the fun parts:

  1. Why, for the life of me, does Visual Studio “censor” exception information from me? In the code below, you will see on the if (InvokeRequired) branch, a try-catch. I log the exception. ex.InnerException is NULL and the provided ex.StackTrace is anemic (only 1 step in it). Now if I comment the try-catch and simply let it crash via the Debugger, I get a much ampler stack trace. Why is that?

  2. To make things worse, neither of the two stack traces versions contain any information about the i18N crash. They just say “The given key was not present in the dictionary.” and give me a stack trace up to the Invoke declaration.

  3. On the else branch (that is, InvokeRequired == false), if I put a try-catch, I can successfully catch my Exception back to the i18n system. As you can see, I tried to send my exception with InnerException back to the InvokeRequired == true branch. However, even so, InnerException stays NULL there and I cannot access my i18N error.

I am puzzled by all these things and maybe somebody can help shed some light over here. If you got really strong lanterns that is.

Here is the function’s code.

private delegate void AddMessageToConsole_DELEGATE (frmMainPresenter.PresenterMessages message);
private void AddMessageToConsole (frmMainPresenter.PresenterMessages message)
{
  if (InvokeRequired)
  { //Catching any errors that occur inside the invoked function.
    try { Invoke(new AddMessageToConsole_DELEGATE(AddMessageToConsole), message); }
    catch (Exception ex) { MSASession.ErrorLogger.Log(ex); }
    //Invoke(new AddMessageToConsole_DELEGATE(AddMessageToConsole), message);
  }
  else
  {
    string message_text = ""; //Message that will be displayed in the Console / written in the Log.
    try
    {
      message_text = I18N.GetTranslatedText(message)
    }
    catch (Exception ex)
    {
      throw new Exception(ex.Message, ex);
    }

    txtConsole.AppendText(message_text);
  }
}
  • 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-16T11:11:44+00:00Added an answer on May 16, 2026 at 11:11 am

    The call stack problem is a known issue with Control.Invoke. You lose the call stack. Sorry. This is because it is rethrown on the UI thread using throw ex;.

    The best solution would be to replace the background thread with a background Task. Note: this solution is only available for .NET 4.0. The Task class properly marshals exceptions. I wrote a blog entry about reporting progress from tasks, and the code in that blog entry will allow you to catch any UI update errors in the background thread, preserving the original exception and its call stack.

    If you can’t upgrade to .NET 4.0 yet, there is a workaround. Microsoft’s Rx library includes a CoreEx.dll which has an extension method for Exception called PrepareForRethrow. This is supported in .NET 3.5 SP1 and .NET 4.0 (and SL 3 and SL 4). You’ll need to wrap your UI updater method with something a little uglier:

    private delegate void AddMessageToConsole_DELEGATE (frmMainPresenter.PresenterMessages message); 
    private void AddMessageToConsole (frmMainPresenter.PresenterMessages message) 
    { 
      if (InvokeRequired) 
      {
        // Invoke the target method, capturing the exception.
        Exception ex = null;
        Invoke((MethodInvoker)() =>
        {
           try
           {
             AddMessageToConsole(message);
           }
           catch (Exception error)
           {
             ex = error;
           }
        });
    
        // Handle error if it was thrown
        if (ex != null)
        {
          MSASession.ErrorLogger.Log(ex);
    
          // Rethrow, preserving exception stack
          throw ex.PrepareForRethrow();
        }
      } 
      else 
      { 
        string message_text = ""; //Message that will be displayed in the Console / written in the Log. 
        try 
        { 
          message_text = I18N.GetTranslatedText(message) 
        } 
        catch (Exception ex) 
        { 
          throw new Exception(ex.Message, ex); 
        } 
    
        txtConsole.AppendText(message_text); 
      } 
    } 
    

    Note: I recommend you start a migration away from ISynchronizeInvoke. It is an outdated interface that is not carried forward into newer UI frameworks (e.g., WPF, Silverlight). The replacement is SynchronizationContext, which supports WinForms, WPF, Silverlight, ASP.NET, etc. SynchronizationContext is much more suitable as an abstract “thread context” for a business layer.

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

Sidebar

Ask A Question

Stats

  • Questions 500k
  • Answers 500k
  • 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 I think Alex's answer won't work because strings are immutable… May 16, 2026 at 1:50 pm
  • Editorial Team
    Editorial Team added an answer (Sorry, I originally misread the requirements thinking it was the… May 16, 2026 at 1:50 pm
  • Editorial Team
    Editorial Team added an answer Yes, use the \Q and \E escapes: #!/usr/bin/perl use strict;… May 16, 2026 at 1:50 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

I'm working with a DataGridView (Windows Forms) with MultiSelect enabled which is placed in
This is a strange driver error which doesn't make a lot of sense to
The answer to this question may turn out to be, Don't use typed DataSets
Since switching to Windows 7, we've noticed that several of our web pages have
Let me start from a real life example: Customer: Alex, just noticed something strange
I noticed that a lot of tutorial instructions often have this code: $sql=SELECT *
Strange one here, hoping to get some feedback to point me in the right
When I use Google Chrome to make an AJAX POST request I get extra
I've got a strange problem. Let me first describe my situation: Webserver01 (Webapplication, SQL,
Boy do I have a strange bug. I have a website that needs 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.