WPF program to start loading splash screen Wait method

Source: Internet
Author: User

Recently do the project, point to open the WPF program, the interface is not open for a while, to the interface before coming out, so think of it to make a splash window.
The first thought is that when the program starts, a new window (with animation waiting) is invoked, and the show method does not block the following to display. As shown below:

Namespace Wpf_splashtest
{
    ///<summary>
    ///interaction logic for App.xaml
    ///</summary > Public
    Partial class app:application
    {
        protected override void Onstartup (StartupEventArgs e)
        { C8/>splashwindow SW = new Splashwindow ();
            Sw. Show ();
            Base. Onstartup (e);
        }
    }

When the main program starts, it handles time-consuming operations in the Load method, which uses a sleep 10s to simulate time-consuming operations:

private void Window_Loaded (object sender, RoutedEventArgs e)
        {
            //simulate the time that the main form handles load consumption
            thread.sleep (10000);
        }

after this process, the main program was found to sleep, splash window animation was stuck. The reason is that splash window and MainWindow are windows of the same thread, and the primary thread cannot respond to the interface update when sleeping
Judging by personal experience, it seems that the WPF window does not run in multiple threads, otherwise the error is not WinForm. So the data was checked, and the WPF window could run in a single thread outside the main thread and not run on multiple threads, so the following answer was available:

public partial class App:application
    {
        protected override void Onstartup (StartupEventArgs e)
        {
            Thread t = new Thread (() =>
            {
                Splashwindow sw = new Splashwindow ();
                Sw. ShowDialog ();//cannot use show
            });
            T.setapartmentstate (ApartmentState.STA);/Set single thread
            T.start ();

            Base. Onstartup (e);
        }
    

The above annotation cannot use show, because after using show not to block, then the new thread runs, the line Chengri Splash window will be destroyed, splash window will only be short-lived
How to close Splash window after the form is loaded. To store the SW and then process it in the main program, see:

public partial class App:application
    {public
        static dictionary<string, object> Dic = new Dictionary<st Ring, object> ();
        protected override void Onstartup (StartupEventArgs e)
        {
            thread t = new Thread (() =>
            {
                Splashwindow SW = new Splashwindow ();
                dic["Splashwindow"] = sw;//storage
                SW. ShowDialog ();//cannot use show
            });
            T.setapartmentstate (ApartmentState.STA);/Set single thread
            T.start ();

            Base. Onstartup (e);
        }
    

When the main form is loaded, it handles:

private void Window_Loaded (object sender, RoutedEventArgs e)
        {

            //simulate the time that the main form handles load consumption
            thread.sleep (10000);

            if (App.Dic.ContainsKey ("Splashwindow"))
            {
                Splashwindow sw = app.dic["Splashwindow"] as Splashwindow;               
                Sw. Dispatcher.invoke (Action) (() => SW. Close ());//Turn off Splashwindow} on SW threads
        

Sw. Dispatcher.invoke (Action) (() => SW. Close ()), it must be turned off on the splash window's thread, or it will be a cross threading action form that can be faulted.
The above ideas can be in the main thread of window_loaded bold time-consuming operation, without fear of the phenomenon of suspended animation, such as wrong understanding, welcome to correct
Although this example does not have a map, but the download resources are absolutely pleasantly surprised: there are stolen from elsewhere in the WPF Win8 style of waiting control oh (I found it a long time to find), click here to download this instance resource

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.