-1

In my solution, my main project is a WPF c# project. Within the solution, there is a legacy project that I needed to integrate that is a VB winforms project. This was done via a wrapper class in the WPF project that manipulates some public objects from my legacy project.

In my WPF project, I have implemented Application_DispatcherUnhandledException in order to have exceptions bubble up to here, where I log them in our database for debugging.

The problem that I have is that whenever something from the legacy project crashes (such as a NullReferenceException), the code never reaches our implementation of Application_DispatcherUnhandledException which is in the main WPF project.

Is is possible to redirect all unhandled exceptions from the WinForms project to the WPF one?

PeeP
  • 1
  • 1
  • If the exception never reaches you code then there must be an exception handler in the WinForm project that is capturing the exception. To bubble up the exception you would need to add a 'throw' in the windform exception handler. See : https://stackoverflow.com/questions/2999298/difference-between-throw-and-throw-new-exception – jdweng Jan 30 '18 at 22:21
  • 1
    This question could use more info how this 'integration' is done - exception from managed parts of the code, i.e. a form constructor, will bubble up properly. Exceptions thrown from stuff on the 'messageloop' (pretty much everything that happens after show) - will not. One option would be to set up a catch block in the Main function(you will need to add main manually in WPF), though you should still not expect to catch exceptions thrown in stuff like Paint/Load. – user6144226 Jan 30 '18 at 23:22
  • In my WPF project, I will initialize a class of my wrapper, which in turn calls the constructor of my winForms project. I attach a few events to the winForm object that i handle in my WPF project, allowing me to implement my own logic in my c# code. – PeeP Jan 31 '18 at 14:44
  • When something goes wrong in the code that handles the WinForm event, it crashes in the WinForm project, which my WPF Application_DispatcherUnhandledException is not catching. Instead I get the unhandled error message box that WinForms has be default (this one : https://imgur.com/a/TBbjf ) – PeeP Jan 31 '18 at 14:50
  • Try to look at this https://stackoverflow.com/questions/8148156/winforms-global-exception-handling. – IvanH Jan 31 '18 at 21:45

1 Answers1

0

It would be nice if you could post a an example of how your wrapper looks. And how exactly do you "start your winforms application" Do you have access to the actually Form classes of the project?

While this isn't exactly "bubbling up" - it is possible to write a wrapper that will force exceptions to be "rethrown" on the dispatcher.

internal class WinFormsWrapper
{
    private Form _form;

    public WinFormsWrapper(Form f)
    {
        _form = f;            
    }

    public void Run()
    {
        //Winforms exceptions thrown on the message loop should go here
        //this handler will force them do be "rethrown" on the calling dispatcher
        System.Windows.Forms.Application.ThreadException += (wf_sender, wf_e) => { ExceptionDispatchInfo.Capture(wf_e.Exception).Throw(); };
        System.Windows.Forms.Application.Run(_form);
    }
}
user6144226
  • 613
  • 1
  • 8
  • 15