Close WPF Window From Another Window ContentRendered Event

I have 3 windows.  Window1 opens Window2 in a new thread.  It's just a window that says 'Loading'.  Window1 also opens Window3.  When Window3's ContentRendered fires I want to close Window2.

I have tried numerous things.  Why is this so difficult?  Maybe it isn't and I am just making it this difficult.

     For Each Window In Application.Current.Windows
            If Window = "Window2" Then

            End If

That, of course, fails.  Can someone please tell me how to do this?  It's driving me crazy.  Thanks for any help on this.
G ScottAsked:
Who is Participating?
G ScottConnect With a Mentor Author Commented:
Application.Current.Windows.OfType(Of WindowTitle)().First().Close()

That's how you do it.
You can have a shared array or a data structure to store all active windows.  Once window 3 loads, search the structure for the window and close it.  Or the other option is to use Dispatcher.FromThread.  Check out
G ScottAuthor Commented:
Haha, skills are limited in WPF. I had a mistake in my code up there

 For Each win In Application.Current.Windows
            If win.Name = "winActivity" Then

            End If

This works.

Disregard.  This does not work.  Haha..forgot I added a DispatcherTimer to Window2.  It was closing and I thought I had it.
G ScottAuthor Commented:
I found the answer on my own.
G ScottAuthor Commented:
Took me a while, but I found the answer on my own.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.