Click here to Skip to main content
15,847,750 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
In my WPF project I have a splash screen that I want to stay 10 seconds before the main window. In the splash screen I have  two buttons one for exiting and another for continuing. My intention is to give the user an option to continue to the main window before 10 seconds is passed. However, the splash screen freezes out for whole 10 seconds and won't let the user click the continue button. How can I prevent that?


What I have tried:

public partial class SplashScreen : Window {
    public SplashScreen() {
        InitializeComponent();
        this.Show();
        Sleep(10000);
    }

    private void Sleep(int delay) {
        Thread.Sleep(delay);
        btnContinue_Click(null, null);
    }

    private void btnExit_Click(object sender, RoutedEventArgs e) {
        Application.Current.Shutdown();
    }

    private void btnContinue_Click(object sender, RoutedEventArgs e) {
        MainWindow mainw = new MainWindow();
        mainw.Show();
        Close();
    }
}
Posted
Updated 29-Aug-22 21:49pm

1 solution

When you use Sleep, the current thread does just that: goes to sleep for the specified period and does absolutely nothing else.

So when you call it in your constructor, the main (UI) thread gets frozen beucase it's asleep.

Remove all that, and add a Timer to your Window - set it's interval to 10 seconds, and handle it's Tick event.
In the Handler, close the window.
 
Share this answer
 
Comments
CPallini 30-Aug-22 3:05am    
5.

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