This project is read-only.

Detect closed window

Nov 24, 2011 at 2:52 PM


I am using white with MFC project

I am storing my windows in some array. If I close window with X, I can't use reference to that window any more from my array.

But how I can detect it. I mean there is no any event fired and also methods IsClosed, IsVisible not helping me because I get same values

when this window is minimized. The only way that I found is check DisplayState and in case the window is disposed it will throw NullReferenceException.

Is there any method I can use to get this info without generating exception?

Thank you in advance.

Dec 2, 2011 at 3:14 PM

You are getting the same values for IsVisible(), etc. when the window has been closed and is no longer existing? That sounds like you need to search again through the Desktop.Instance and reload the window array. Maybe I am misunderstanding the issue...

Dec 3, 2011 at 5:58 PM


you are misunderstanding. I want to detect when I need to reload my window via Application.GetWindow. IsVisible amd IsClosed give me same values when window is minimized (and I don't need to reload it) and when it's closed (and I do need to reload it).

Dec 3, 2011 at 6:43 PM

It sounds to me like you are keeping instances of objects that are no longer valid (when the window is closed), and that is why the instances gives you the same values for the Visible, etc. properties. It sounds like you need to do your order of operations differently for the minimized state than for the closed state. It is hard to say without seeing the issue first hand and debugging. We handle our windows in a completely different manner than this, and without looking at your code, it's hard to say...

Dec 3, 2011 at 7:38 PM

Yes , I am keeping instances of all my windows (because I don't want to look for them every time via Application.GetWindow()) So when instance became unavailable (Closed for example) I want to reload it. But I can't detect this moment. IsVisible and IsClosed give me same results no matter window is died or minimized, so I can't detect if I need to reload it. I'll post here my code in two days.

Dec 9, 2011 at 7:43 AM

Did you try using the automation element of the Window to get the IsEnabled property. You can access both the cached as well as current properties. of course if the window is already closed, you will get a null reference exception which u can handle. Please ignore if u have already tried this.

Dec 9, 2011 at 9:20 AM

Have you tried using the IsOffScreen element? I use this when I need to detect that a window is no more but I use this via AutomationElement.Current.IsOffScreen.

It works well for me.


Dec 11, 2011 at 12:25 PM
Edited Dec 11, 2011 at 12:30 PM

Thanks loganathan,


gives false when window is closed and true when it only minimized. (It doesn't throw exception if window is closed)


Window.AutomationElement.Current.IsOffScreen gives true in any case. (Minimized or closed)