C# winforms startup (Splash) form not hiding

后端 未结 4 1116
鱼传尺愫
鱼传尺愫 2020-12-03 00:30

I have a winforms application in which I am using 2 Forms to display all the necessary controls. The first Form is a splash screen in which it tells the user that it it load

4条回答
  •  旧时难觅i
    2020-12-03 00:48

    Probably you just want to close the splash form, and not send it to back.

    I run the splash form on a separate thread (this is class SplashForm):

    class SplashForm
    {
        //Delegate for cross thread call to close
        private delegate void CloseDelegate();
    
        //The type of form to be displayed as the splash screen.
        private static SplashForm splashForm;
    
        static public void ShowSplashScreen()
        {
            // Make sure it is only launched once.
    
            if (splashForm != null)
                return;
            Thread thread = new Thread(new ThreadStart(SplashForm.ShowForm));
            thread.IsBackground = true;
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();           
        }
    
        static private void ShowForm()
        {
            splashForm = new SplashForm();
            Application.Run(splashForm);
        }
    
        static public void CloseForm()
        {
            splashForm.Invoke(new CloseDelegate(SplashForm.CloseFormInternal));
        }
    
        static private void CloseFormInternal()
        {
            splashForm.Close();
            splashForm = null;
        }
    ...
    }
    

    and the main program function looks like this:

    [STAThread]
    static void Main(string[] args)
    {
        SplashForm.ShowSplashScreen();
        MainForm mainForm = new MainForm(); //this takes ages
        SplashForm.CloseForm();
        Application.Run(mainForm);
    }
    

提交回复
热议问题