0

Is it possible to show just splash screen (without showing main form)?

SplashScreen splash;

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    splash = new SplashScreen();
    splash.Show();

    BackgroundWorker backgroundWorker = new BackgroundWorker();
    backgroundWorker.DoWork += BackgroundWorker_DoWork;
    backgroundWorker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
    backgroundWorker.RunWorkerAsync();

    // var mainForm = MainForm();
    // Application.Run(layoutForm); // I don't want to call this from here
}


private static void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
     splash.Close();
     // This never gets called, coz application ended
}

private static void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    for (int i = 0; i <= 100; i++)
    {
        Thread.Sleep(100);
    }
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Saad Shaikh
  • 175
  • 1
  • 13
  • 1
    At least, you need to call `ShowDialog` instead of `Show` and start your background processing before that (assuming that the simple way works for you). – Phil1970 Aug 04 '18 at 17:20
  • 1
    Another thing, you might also want to use some timers for some control over the time the splash is displayed minimally on a fast computer. – Phil1970 Aug 04 '18 at 17:24
  • https://stackoverflow.com/a/32421479/3110834 – Reza Aghaei Aug 04 '18 at 19:02
  • thnx @Phil1970, seems like `ShowDialog` will do the trick for me.. – Saad Shaikh Aug 04 '18 at 21:58
  • I need to move my BackgroundWorker code to SplashScreen Load event – Saad Shaikh Aug 04 '18 at 21:59
  • You are making it harder then it needs to be. Just make your splash screen form your main form, easy peasy. Your BGW's RunWorkerCompleted event handler only needs Application.Exit() to end the program. – Hans Passant Aug 05 '18 at 11:44

1 Answers1

1

You would call your Splash screen from your main form.

public partial class mainform : Form
{
    public mainform()
    {
        InitializeComponent();
    }

    public mainform_Load(object sender, EventArgs e)
    {
        this.Visible = false;
        using (SplashScreen ss = new SplashScreen())
        {
            ss.ShowDialog();
            SetTheme(ss.LoadedTheme);
            this.Visible = true;
        }
    }

    private void SetTheme(Theme theme)
    {
        //Put your theme setting code here.
    }
}

Here's how your SplashScreen code will look:

public partial class SplashScreen : Form
{
    public Theme LoadedTheme { get; private set; }

    public SplashScreen()
    {
        InitializeComponent();
    }

    public void SplashScreen_Load(object sender, EventArgs e)
    {
        bwSplashScreenWorker.RunWorkerAsync();
    }

    public void bwSplashScreenWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        // Load in your data here
        LoadedTheme = LoadTheme();
    }

    public void bwSplashScreenWorker_Completed(object sender, RunWorkerCompletedEventArgs e)
    {
        DialogResult = DialogResult.OK;
    }
}

Now, your application will start, when the mainform is loaded it will hide itself, open the SplashScreen in a blocking manner. The splashscreen will load in your theme data in a background thread and save it into the LoadedTheme property. When the background worker completes it will set the DialogResult to OK which closes the SplashScreen and returns control to mainform_Loaded. At this point you call your SetTheme method passing in the public property LoadedTheme from your SplashScreen. Your SetTheme method sets up your theme and returns back to mainform_Loaded where it sets the mainform to visible.

Handbag Crab
  • 1,488
  • 2
  • 8
  • 12
  • The whole point of not showing main form prior splash screen ends loading is, I am doing something in ctor of `MainForm` which depends on data loaded on startup. :( – Saad Shaikh Aug 04 '18 at 21:55
  • If you're doing something long running in your main form constructor then you are doing something wrong. If it's loading data then do it on the splash screen in the background worker. – Handbag Crab Aug 05 '18 at 09:32
  • No I am not doing any long running task in ctor. I am doing long running task in splash screen background worker. and using the data in MainForm ctor. eg. user's selected theme etc – Saad Shaikh Aug 05 '18 at 09:34
  • Oh I see. Then why not do that work in mainform_Loaded after your splash screen has closed and before you set mainform.Visible to true? It'll have the same effect but will be much cleaner code. I'll edit my answer to include this. – Handbag Crab Aug 05 '18 at 10:00