Replies: 1 comment 4 replies
-
@mqudsi Thanks for your analysis and suggestion. Unfortunately, this is not something we can support for several reasons:
I appreciate the added complexity to client code, but I think your approach of handling Window.VisibilityChanged (or Window.Closed) to know when any window property is safe to access, is best. |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Proposal: Replace
Window.Visible
with cached/proxy valueInstead of directly exposing the WinRT
IWindow.Visible
as a property, use a C# property instead that is updated onIWindow.Closed
andIWindow.VisibilityChanged
to protect against exceptions during teardown.Summary
Currently, accessing
Window.Visible
directly maps to a WinRT/COM operation that throws aCOMException
if theWindow
has been closed. Since UI code may continue to be called until the state quiesces and operations that may queryWindow.Visible
may already be taking place mid-flight, it would be safer for this property to provide cached/proxied window visibility state and for reading it to be a simple read operation that does not cross ABI boundaries and does not depend on the window handle still being valid.Rationale
Window.Visible
is defined as a property and not a function; it is not the expectation that its execution will be crossing ABI boundaries and that it would be performing "meaningful" work;Window
has not been cleaned up by the GC), access to its properties would be valid (though other operations might - of course - throw after the window has closed)Window
facade aroundIWindow
should make every reasonable (non-expensive and straightforward) effort to make it seem like you are interacting with a native C# object and accessing a property leading to aCOMException
being thrown flies in the face of that.Window.Visible
repeatedly in their UI update code, it is not an unreasonable expectation that this is a quick and fast operation that does not incur much cost. Crossing ABI boundaries to query a value that rarely changes contradicts that, especially when there exists an easy fix for this.Important Notes
This is what I have in my own project to protect against this:
Obviously this is not a sufficient solution in that it doesn't protect against
Window.Current.Visible
being called and must be manually copied to eachWindow
class in a project.Open Questions
Window.IsVisible
in the sample code above to reflect a change if the call races with the execution of theVisibiltyChanged
callback handler) but since theIWindow.Visible
property is non-synchronized and non-atomic (the actual state of the window's visibility may change immediately after the call determines the return value, resulting in stale information being returned), this does not seem materially different.Beta Was this translation helpful? Give feedback.
All reactions