Error: Cannot set Visibility to Visible or call Show, ShowDialog, Close, or WindowInteropHelper.EnsureHandle while a Window is closing.
See original GitHub issueDescribe the bug
Disclaimer: this is a bit vague but thought I’d bring this up as I see this error frequently in my logs initiated off a Window button close.
I haven’t been able to track this one down myself for debugging in Markdown Monster, but I see this error quite frequently in my logs. I believe this is caused when the close box is clicked on the window perhaps when another shut down operation is already in place, or when a forced shutdown occurs due to an unhandled exception and app shutdown dialog.
Here’s the stack trace:
To Reproduce Can’t reproduce reliably - that’s where the vagueness comes in.
Environment(please complete the following information):
- MahApps.Metro 1.6.5
- OS: Win10 1903
- Visual Studio 2019
- .NET Framework 4.8
other:
I’m guessing this is due to calling Close()
when the window is already closing, so perhaps an additional check can be performed before calling Window.Close()
closing the window.
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (1 by maintainers)
Looks like with the simple changes preventing re entrancy the errors in the log have stopped… I haven’t seen any repeats in recent version logs which is great.
@timunie Ah, thought the code posted here was the code contained in the showcase application.
@RickStrahl I can fully reproduce your issue and you can too. Just double click on the close window button in MarkdownMonster. It’s possible to cause this crash in MarkdownMonster because you are doing way too much in your closing handler and even issue
DoEvents
in there.DoEvents
breaks the control flow. Even showing dialogs might cause these kinds of issues. The way i solved this was to check for a flag inClosing
and always sete.Cancel
if that flag isn’t set. To set the flag i call the code for setting the flag viaDispatcher.BeginInvoke
and also check for re-entrancy to prevent the code to set the flag from running multiple times before one call completely finished. As you are doing quite a lot in closing i’d also add some kind of busy adorner/marker.