Fighting the "InteropServices.SEHException: External component has thrown an exception" blues

<8/18/2004 update: see part II of this post, with a better solution: https://weblogs.asp.net/asanto/archive/2004/08/18/216825.aspx  >

.Net version 1.1 has a bug with XP Visual Styles which causes the following cryptic exception: "'System.Runtime.InteropServices.SEHException: External component has thrown an exception". This exception is trapped in the main application message loop (in a winform this is typically "Application.Run(new Form1()); " ) which really doesn't help when trying to debug.  It turns out that this is a known bug which has been written up in many a blog: Scott HanselmanJeff Key, Aaron Z.   

Most blogs recommend either adding a DoEvents() in the code immediately after the EnableVisualStyles() command, or to skip the EnableVisualStyles()  altogether and instead use an XML manifest to enable XP themes.  The DoEvents() didn't work for me at all (my app still crashed, just a bit deeper inside the UI) and I'm not willing to even think about using an XML manifest.

The solution I came up is:

[STAThread]

static void Main()
{
Application.EnableVisualStyles();
     new Thread(new ThreadStart(AppRun)).Start();
}

private static void AppRun()
{
Application.Run(new MainForm());
}

To be honest, this code sort of scares me. It works (so far) but I can't explain exactly why.  Help ??!?

Comments

  • Anonymous
    August 13, 2004
    Put in Thread.Sleep(10000); the code will be a little cleaner :) trade off with speed. hehe
  • Anonymous
    August 13, 2004
    Err, yeah, thanks. right. Just what I was looking for. They should have put that in a KB :)
  • Anonymous
    August 13, 2004
    The comment has been removed
  • Anonymous
    August 13, 2004
    Thanks alot for the links, Nat. I checked them out and I'll use them as a last resort. The process is too painful to do manually - the second article helps automate some of it, but would still require resigning, etc. I'm currently using VS for my builds, so without moving to NANT I think it will be difficult to come up with a reasonable compile process.

    Thanks again for the help!
  • Anonymous
    August 14, 2004
    There is a way to automate it :)
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/xpthemeaddin_.asp

    It might help :) unless you move to NAnt.... So sad i'm using NAnt and there is no workaround for a moment. I might write NAnt Task some days :)