Nasty Bug in EnableVisualStyles - Help!

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I have a main form (C#) which, when a button is pressed, I create a modal dialog (ShowDialog). This dialog has a combobox on it. In the eventhandler of SelectedIndex changed on this comboxbox I create another modal dialog which the user can dismiss. When that modal dialog returns, the whole application crashes with 'System.Runtime.InteropServices.SEHException: External component has thrown an exception.'

I've traced this (eventually) to Application.EnableVisualStyles() (followed by Application.DoEvents()). Take these lines out and it works....(as expected)...put them in and it fails miserably!

Anyone seen this before/got any workarounds?

Thanks

Jon
 
I'm having a similar problem, but never thought of suspecting
EnableVisualStyles() as the culprit. I'll try commenting it out in in my app
and see what effect it has.

Tom Dacon
Dacon Software Consulting

Jon said:
Hi

I have a main form (C#) which, when a button is pressed, I create a modal
dialog (ShowDialog). This dialog has a combobox on it. In the eventhandler
of SelectedIndex changed on this comboxbox I create another modal dialog
which the user can dismiss. When that modal dialog returns, the whole
application crashes with 'System.Runtime.InteropServices.SEHException:
External component has thrown an exception.'
I've traced this (eventually) to Application.EnableVisualStyles()
(followed by Application.DoEvents()). Take these lines out and it
works....(as expected)...put them in and it fails miserably!
 
Bingo! Same problem, same solution: I commented out the EnableVisualStyles()
call and the problem went away.

In my case, I bring up a modal dialog from within a user control's event
handler, do some processing after the modal dialog returns, and the
unhandled exception occurs when the event handler returns control back to
the runtime.

If you're need or want to keep the visual styles and are interested in a
work-around, read on:

If you decouple the modal dialog and the accompanying processing from the
event handler in which you're currently showing the dialog, the problem can
be made to go away, even with EnableVisualStyles() being called. The way I
do this is with a timer. I set up a Windows.Forms.Timer with a short
interval such as 100 ms. In the event handler in which I would ordinarily
run the dialog, I store relevant data in instance memory and then start the
timer running. In the timer's Tick event I retrieve the data from instance
memory, bring up the dialog, and follow with the appropriate processing.

If you're interested in the coding details, email me offline ( tom at
dacons dot com )and I'll send you some code. I've generalized the process
to some extent into a design pattern I call 'deferred command processing',
so that the same technique can be used from multiple places within the
control's code.

Tom Dacon
Dacon Software Consulting
 
In my case, I bring up a modal dialog from within a user control's event
handler, do some processing after the modal dialog returns, and the
unhandled exception occurs when the event handler returns control back to
the runtime.

That's a nasty bug indeed! Your workaround is quite sophisticated but
wouldn't it be easier to simply revert to the .NET 1.0 way of enabling
XP styles, i.e. using a manifest file?
 
You're right.....I had a 'light-bulb-moment' and tried the manifest file and it works a treat now.....hopefully MS will have fixed this in the next service-pack/release!
 
That's a nasty bug indeed! Your workaround is quite sophisticated but
wouldn't it be easier to simply revert to the .NET 1.0 way of enabling
XP styles, i.e. using a manifest file?

Well, you know, you're right - it would have been easier, now that it's
clear where the problem is coming from. Actually I never tipped to the idea
that the enable visual styles call was the problem, so I had my solution
already all worked out when Jon's post prompted me to see if it was my
problem too. Sure enough it was. It's comforting when the sources of
nebulous problems like that eventually "manifest" themselves. One less
puzzle to worry about in the dark hours of the night...

Regards,
Tom
 
Back
Top