wixsharp icon indicating copy to clipboard operation
wixsharp copied to clipboard

UnhandledException event

Open AmolNagare7003 opened this issue 10 months ago • 3 comments

Hi,

I am trying to catch Unhandled exceptions in UnhandledException event, when I am trying to do that event is not getting triggered.

I am attaching sample application here, in that I have explicitly tried to throw exception from welcome dialogue.

My expectation is to trigger UnhandledException event in case of any exception from ManagedUI, Custom Actions etc.

WixSharpTest.zip

AmolNagare7003 avatar Mar 11 '25 09:03 AmolNagare7003

looking into it

oleg-shilo avatar Mar 12 '25 11:03 oleg-shilo

Indeed it is a problem.

Because of some UI threading error handling intricacies, the WixSharp error handlers are called only for non-UI scenarios. Even though WixSharp executes the UI thread proc inside of the try-catch statement. Only by debugging the WinForm native assemblies, I found that the default implementation of Application.OnThreadException is called regardless of the try-catch wrapping. I cannot explain that. But... it showed the work around.

The next release will have an additional UIThread error handling that will redirect the exception handling to the user supplied EnhandledException handling routine.

In a mean time you will be able to achieve the same workaround directly in your code by adding Application.ThreadException to the Project_UIInitialized event handler:

private static void Project_UIInitialized(SetupEventArgs e)
{
    if (!new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator))
    {
        e.Result = ActionResult.Failure;
        var startInfo = new ProcessStartInfo();
        startInfo.UseShellExecute = true;
        startInfo.WorkingDirectory = Environment.CurrentDirectory;
        startInfo.FileName = "msiexec.exe";
        startInfo.Arguments = "/i \"" + e.MsiFile + "\"";
        startInfo.Verb = "runas";
        Process.Start(startInfo);
    }
    else
    {
        Application.ThreadException += (s, arg) =>
        {
            MessageBox.Show(arg.Exception.Message, "Unhandled UIThread Exception");
        };
    }

}

oleg-shilo avatar Mar 15 '25 09:03 oleg-shilo

Thanks Oleg for workaround which you have suggest.

AmolNagare7003 avatar Mar 17 '25 06:03 AmolNagare7003

Hey, I came across a problem related to the changes from this issue. Maybe it's enough to address this on the wiki, or maybe something needs to be changed in the code.

Anyway, I have a new project where Caliburn.Micro is used based on older templates (though the same situation occurs with other DLLs that need to be included in the msi file). The installer crashed right after launching, and the exception in the logs was completely unreadable:

ManagedUI unhandled Exception: System.Threading.ThreadExceptionEventArgs

It turned out that the problem was the missing required DLLs, but this was only determined by rolling back to a version before this change. In that earlier version, a dialog would appear on startup indicating issues with loading the library.

Dersei avatar May 27 '25 12:05 Dersei

Hey @Dersei, this exception is a generic exception that is created by a thread when an unhandled exception occurs in a thread that is not main.

I would prefer t catch it in WixSharo and report with the actual inner exception info. Can you share the exception stack trace, if it is available? I want to see what is the point where I need to intercept it.

oleg-shilo avatar May 29 '25 12:05 oleg-shilo