Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

Let's say we have a Thread :
C#
Thread mainWindow = new Thread(new ThreadStart(StartMainWindow));

Wich start MainWindow :
C#
private void StartMainWindow()
        {
            MainWindow main = new MainWindow();
            main.Show();            
        }


How can I hide the first window from the second after some treatments

The idea is to hide the second window and swap them at the end of treatment

What I have tried:

Maybe I will use only one window but I want to know if this is possible
Posted
Updated 7-Nov-18 23:15pm
Comments
Richard MacCutchan 19-Oct-18 3:37am    
Why do you need separate threads? Just use the Show and Hide methods.
Dave Kreskowiak 19-Oct-18 8:25am    
All UI work MUST be done on the thread your application was started on. You cannot have multiple threads with their own windows.
johannesnestler 22-Oct-18 10:16am    
Hi Dave, Sry but this is not true. I can have multiple threads with their own Windows (e.g. WindowsExplorer works like this) See https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-3.5/ms741870%28v%3dvs.90%29 for an example (it's quite old, just the first I found)
Dave Kreskowiak 22-Oct-18 10:42am    
Whoops. I didn't see the WPF tag on the post. Sorry.
johannesnestler 22-Oct-18 10:19am    
I don't think you have to use multiple UI-Threads for this case - I support Richards comment - just use Show and Hide (Sounds like you want some sort of Splash or Progress screen..)
But it's technically possible to have multiple UI threads, but many programmer think it's not - because it's a very rare thing to do...)

1 solution

Hello!

As I remember WPF Dispatcher thread is main thread, whose handling all UI control. So if you are going to render or control any UI component; you have to use Dispatcher thread.

Now came back to your question. As I understand, you have 2 windows, and you want to hide the main window with another one. In other words, you are actually changing main window.
This can be achieved by getting Current App dispatcher thread, and set current window.
e.g.

C#
Application.Current.Dispatcher.Invoke((Action)delegate
{
   SecondWindow window = new SecondWindow ();
   App.Current.MainWindow.Close();
   App.Current.MainWindow = window;
   App.Current.MainWindow.Show();
});


Note that I am Using
C#
Application.Current.Dispatcher.Invoke
because I am invoking from secondary thread (using Task). Otherwise you are not allowed to play with UI controls from secondary thread
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900