Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 views
in Technique[技术] by (71.8m points)

winforms - Splash screen display method best practice C#

I am showing a splash form by starting a new thread immediately before running my main form.

In the method that is run by this thread, I am using Application.Run as shown in Option 1 below. Is this a correct way of doing this, or are there problems waiting for me becaue I have called Application.Run twice? An alternative is Option 2, also shown below where I call .ShowDialog() to display the form.

The splash form itself closes after a specified time, controlled within the form itself, and both options appear to work well.

So my question is: Which is preferred - Option 1 or Option 2? If you could give specific reasons for one or the other that would be great.

Thanks.

Snippet of Main:

// Run splash screen thread.
Thread splash = new Thread(new ThreadStart(ShowSplash));
splash.Start();

// Run main application.
Application.Run(new MainForm());

Show splash form option 1:

    static void ShowSplash()
    {
        Application.Run(new SplashForm());
    }

Show splash form option 2:

    static void ShowSplash()
    {
        using (SplashForm splash = new SplashForm())
        {
            splash.ShowDialog();
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Option 2 will probably run into trouble because then you are using the same Mesageloop as the MainForm but from another thread.

Option 1 is fine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...