Asked  7 Months ago    Answers:  5   Viewed   60 times

Sometimes, under not reproducible circumstances, my WPF application crashes without any message. The application simply close instantly.

Where is the best place to implement the global Try/Catch block. At least I have to implement a messagebox with: "Sorry for the inconvenience ..."



You can handle the AppDomain.UnhandledException event

EDIT: actually, this event is probably more adequate: Application.DispatcherUnhandledException

Tuesday, June 1, 2021
answered 7 Months ago

Here is some pretty robust code we advise people to use when they're implementing in their Windows Applications.

namespace YourNamespace
    static class Program

        static void Main()
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Application.Run(new Form1());

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)

        static void HandleException(Exception e)
            //Handle your Exception here




Wednesday, June 23, 2021
answered 6 Months ago

This is the expected behavior.

The difference you see is a result of the application being run with the debugger attached. When you launch it from within Visual Studio, the debugger is automatically attached (unless, of course, you choose to "Start Without Debugging"). That disables the built-in exception handler, which is the one responsible for showing you the "vanilla" .NET exception dialog. Launching it from outside VS doesn't attach the debugger, leaving the built-in exception handling enabled. (Note that this has nothing to do with compiling your program in "Debug" mode versus "Release" mode.)

See the accepted answer to this related question for more information. I do not believe the difference between VB.NET and C# is relevant in this case.

As that answer mentions, there is a way to disable the built-in exception handler. But before choosing to do so, I recommend reconsidering your approach. Rather than wrapping your entire Main method in a try-catch block, which sounds like a bit of a code smell to me, you might consider handling the built-in AppDomain.UnhandledException event. Jeff Atwood posted a great article here on Code Project about how to replace the standard .NET exception handling with your own more user-friendly method. The solution he proposes has become only that much more elegant as later versions of the .NET FW have improved how the AppDomain.UnhandledException event is handled.

Saturday, August 14, 2021
answered 4 Months ago
  • The name is a bit misleading, because using that method will set a default exception handler for all threads.
  • Make sure no exceptions can be thrown from your exception handler.
  • If you're doing GUI stuff from your exception handler, make sure you're doing it from the right thread.
  • An uncaught exception will only stop the thread where the exception took place, if that also causes the process to terminate depends on any other threads that might be running.
Wednesday, September 22, 2021
answered 2 Months ago

UPDATED: To observe the exception in the other thread, you want to use a Task, queue it to the Dispatcher thread (using TaskScheduler.FromCurrentSynchronizationContext), and wait on it, as such:

var ui = TaskScheduler.FromCurrentSynchronizationContext();
Action doit = () => 
    var error = Task.Factory.StartNew(
        () => { throw new InvalidOperationException("test"); },

    try { 
    } catch (Exception ex) { 
doit.BeginInvoke(null, null); 

UPDATED (again): Since your goal is a reusable component, I do recommend moving to a Task-based interface or something else based on SynchronizationContext such as the event-based asynchronous pattern, instead of basing the component on Dispatcher or ISynchronizeInvoke.

Dispatcher-based components only work on WPF/Silverlight; ISynchronizeInvoke-based components only work on Windows Forms. SynchronizationContext-based components will work with WPF or Windows Forms transparently, and (with a bit more work) ASP.NET, console apps, windows services, etc.

The event-based asynchronous pattern is the old recommended way of writing SynchronizationContext-based components; it's still around for .NET 3.5-era code. If you're on .NET 4, though, the task parallel library is much more flexible, clean, and powerful. The TaskScheduler.FromCurrentSynchronizationContext uses SynchronizationContext underneath, and is the New Way to write reusable components that need this kind of synchronization.

Sunday, September 26, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :