TK.
TK.

Reputation: 47873

c# one Form blocking another in a winform Project

I have an application which uses 2 forms, a Main Form and a Splash Form being used in the following configuration:

public class MainForm : Form
{
    public MainForm()
    {
       SplashScreen splash = new SplashScreen();

       // configure Splash Screen
    }
}


public class SplashScreen
{
    public SplashScreen()
    {
       InitializeComponent();

       // perform initialization

       this.ShowDialog();
       this.BringToFront();
    }
}

NB: Main form is created with the following code:

Application.Run( new MainForm() );

The problem above is that the configuration of splash does not occur unless splash is closed with

splash.Close();

only when this occurs does the rest of the MainForm constructor run. how can I easily stop this blocking behaviour?

Upvotes: 0

Views: 4611

Answers (4)

Grzenio
Grzenio

Reputation: 36649

I already replied to you with a working example on the other question you asked for the same thing:

C# winforms startup (Splash) form not hiding

Upvotes: 1

Joe Doyle
Joe Doyle

Reputation: 6383

Basically, you want to just show you splash form, but not let it block the main form.

Here's how I've done it:

class MainForm : Form {

    SplashScreen splash = new SplashScreen();  //Make your splash screen member

    public MainForm()
    {
        splash.Show();  //Just show the form
    }

}

Then in your MainForm_Load you do your initialization as normal.

Now when your form is ready to be displayed (MainForm_Shown):

public MainForm_Shown()
{
    splash.Close();
}

This lets your MainForm load normally while displaying the splash screen.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062650

Generally, you need to show splash screens on a separate thread, and let the primary thread carry on with loading. Not trivial - in particular, you will need to use Control.Invoke to ask the splash screen to close itself when ready (thread affinity)...

Upvotes: 1

Yes - that Jake.
Yes - that Jake.

Reputation: 17121

Use splash.Open() rather than splash.OpenDialog() and this won't happen.

Upvotes: 0

Related Questions