0

in my splash code i get an error. This is a separate form to the regular.

    public Form1()
    {

        InitializeComponent();
        Thread t1 = new Thread(new ThreadStart(SplashForm));
        t1.Start();
        Thread.Sleep(5000); // The amount of time we want our splash form visible
        t1.Abort();
        Thread.Sleep(1000);
    }

Basically on the part that says (new ThreadStart(SplashForm));

I get an error on "SplashForm".

The splash form is the name of the form. It says it is a type but is used as a variable.

Any ideas?

Oliver K
  • 15
  • 3
  • `ThreadStart` constructor takes the name of a method, not a type (your form is a type). – vulkanino Mar 09 '12 at 09:25
  • That is not a good way to implement a Splash Screen as you will still end up with your main Form hanging in the background and delayed loading once the Splash Screen is finished. Google C# Splash Screen, 80,000+ results. – Lloyd Mar 09 '12 at 09:25
  • possible duplicate of [C# Splash Screen Problem](http://stackoverflow.com/questions/392864/c-sharp-splash-screen-problem) – Hans Passant Mar 09 '12 at 10:33

2 Answers2

0

You have to provide a method name on constructor of the thread, not a type (SplashForm). You could do something like this:

public Form1()
{
    InitializeComponent();
    new SplashForm().ShowDialog();
}

and in the SplashForm class:

class SplashForm : Form
{
     public SplashForm()
     {
         InitializeComponent();
         new Thread(run).Start();
     }

     private void run()
     {
         Thread.Sleep(5000);
         this.Invoke((MethodInvoker)delegate
         {
             this.Close();
         });
     }
 }

If you get the exception with calling from another thread, you should try the trick with MethodInvoker.

Mihai
  • 600
  • 7
  • 16
  • Wonderful, 1up but one issue.. I get an exception: An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll Additional information: Cross-thread operation not valid: Control 'SplashForm' accessed from a thread other than the thread it was created on. – Oliver K Mar 09 '12 at 09:46
  • Googeling the error message would bring up multiple results that describe the fact that you can not change user interface elements from a thread that did not create the element and that you should use `Control.Invoke` to do so. – Thorsten Dittmar Mar 09 '12 at 09:54
  • I cant figure out how to do it, because its not an element that you click on.. im so confused :/ – Oliver K Mar 09 '12 at 10:16
  • Really sorry for the delay, you should try the edited answer. – Mihai Mar 10 '12 at 09:27
0

If your .NET version is >=3.5 you can use the SplashScreen class:

SplashScreen ss = new SplashScreen ("resource name");
ss.Show(true); // autoclose

It is not necessary to write any code to display a splash screen.

Starting in Visual Studio 2008 SP1, you can quickly configure a splash screen with default settings.

Anyhow your code is wrong because the ThreadStart constructor takes the name of a method, not a type (your form is a type).

vulkanino
  • 9,074
  • 7
  • 44
  • 71
  • 1
    More than .Net 3.5 requirement, `SplashScreen` requires the application to be a WPF app. As the OP is talking about `Forms`, I think he won't be able to use this. – ken2k Mar 09 '12 at 09:35