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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:16:38+00:00 2026-05-23T00:16:38+00:00

Note: I don’t do .NET programming regularly. Normally I do native/MFC, so I’m a

  • 0

Note: I don’t do .NET programming regularly. Normally I do native/MFC, so I’m a bit lost as how to do this properly in the context of C#.

We’re displaying .NET control in the context of a native MFC application. That means the GUI thread is a native thread calling into the WndProc of the .NET control. (Well, at least as far as I can tell.)

Obviously, we do not want to throw exceptions from our (GUI) event handlers, as there is no proper handler down the call stack that would catch them.

As far as I can tell, the solution(s) with AppDomain / UnhandledExceptionEventHandler do not make sense in a native MFC application. (Please correct me if I’m wrong.)

So back to the question: How can I avoid having to add a try/catch block to each event handler of the C# control code? Is there some place (System.Forms…Control.WndProc maybe?) where I can catch all .NET exceptions and just display an error dialog to the user?

  • 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-23T00:16:39+00:00Added an answer on May 23, 2026 at 12:16 am

    You can reduce boilerplate code by using a functional approach. Write a function like this:

    public class Util
    {
        public static void SafeCall(Action handler)
        {
          try
          {
               handler();
          }
          catch(Exception ex)
          {
             // process ex here
          }
    }
    

    And reuse it in every of your GUI event handlers:

    void MyEvent(object sender, EventArgs e)
    {
        Util.SafeCall(()=>NameOfYourHandlerMethod(sender,e));
    };
    

    or

    void MyEvent(object sender, EventArgs e)
    {
        Util.SafeCall(
        delegate
        {
          // enter code here
        });
    };
    

    This might need some additional work to get the sender/EventArgs parameters the way you want them, but you should get the idea.

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

Sidebar

Related Questions

Note: This was posted when I was starting out C#. With 2014 knowledge, I
Note: Originally this question was asked for PostgreSQL, however, the answer applies to almost
Note : The code in this question is part of deSleeper if you want
(Note: This is for MySQL's SQL, not SQL Server.) I have a database column
Note This is not a REBOL-specific question. You can answer it in any language.
Note:I don't mean some theoretical question which don't have any implementation just languages that
Having a PrintTicket how to display the printer-specific configuration dialog? Note: I don't mean
NOTE: XMLIgnore is NOT the answer! OK, so following on from my question on
NOTE: I am not set on using VI, it is just the first thing
Note that I am not asking which to choose (MVC or MVP), but rather

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.