Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to redirect to a new wpf window after 10 seconds?I am trying to use windows.hide() for current window and windows.show() for another window but this doesn't work.

I wrote the code below...

I want to redirect from MainWindow to Window1 after 10 seconds.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UIPack
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        
        
      
        public MainWindow()
        {
            Window1 w = new Window1();
            InitializeComponent();
            System.Threading.Thread.Sleep(10000);
            this.Hide();
            w.Show();
        }
    }
}


But in the case above the MainWindow doesnt appear at all and Window1 appears after 10 seconds
Posted
Updated 21-Jul-13 3:42am
v2
Comments
[no name] 21-Jul-13 9:28am    
Show and Hide do in fact work but without seeing your code we could not possibly tell you what you have done to make you think that it does not work.
suhanaarora 21-Jul-13 9:43am    
Thanks...I updated the question with the code
[no name] 21-Jul-13 10:12am    
The reason that it is doing that is because your MainWindow is not shown at all. The MainWindow cannot show itself before it is even constructed. If you want to show another window after the main window is shown, then when the main window is shown, start a 10 second timer.

1 solution

Waiting 10 seconds is easy, especially if you want the Thread.Sleep (as per your code above) sort of behaviour (but better yet, achieved via a timer that will call your function etc, so that the user or the application can perform actions in those 10 seconds).

to limit the DispatcherTimer to only once, you can add this code in dispatcherTimer_Tick:
C#
(sender as DispatcherTimer).Stop();

Now, switching the WPF window - see the following Q&A that shows HowTo:
how-to-switch-wpf-windows[^]

Cheers,
Edo
 
Share this answer
 
v2
Comments
suhanaarora 21-Jul-13 10:35am    
Hi I wrote following code using timer but in this case Window1 appears after every 5 seconds because timer tick event is raised after every 5 seconds.How to limit this to only once

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace UIPack
{
///
/// Interaction logic for MainWindow.xaml
///

public partial class MainWindow : Window
{

System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();




public MainWindow()
{
InitializeComponent();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 5);
dispatcherTimer.Start();


}

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
Window1 w = new Window1();
this.Hide();
w.Show();

}
}
}
Joezer BH 21-Jul-13 10:39am    
Add this code in dispatcherTimer_Tick :
(sender as DispatcherTimer).Stop();
suhanaarora 21-Jul-13 10:42am    
yeah it works fine now :).Thanks

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