views:

362

answers:

2

Hello, I'm using a Splash Screen from Here. I love how simple it is. But the problem with it is that the spash screen doesn't go away until I click on it. When run within the IDE it works fine. Any ideas? I'd attach the code here but its not inserting properly for some reason.

private System.Windows.Forms.Timer timer1;
    //private Splash sp=null;

    public Form1()
    {
        InitializeComponent();

        Thread th = new Thread(new ThreadStart(DoSplash));
        //th.ApartmentState = ApartmentState.STA;
        //th.IsBackground=true;
        th.Start();
        Thread.Sleep(3000);
        th.Abort();
        Thread.Sleep(1000);
    }

    private void DoSplash()
    {
        Splash sp = new Splash();
        sp.ShowDialog();
    }

    private void timer1_Tick(object sender, System.EventArgs e)
    {
    //      sp.Close();
    }
+1  A: 

That's an ugly implementation. You should check out this SO thread. That describes how to use the VisualBasic.NET namespace from C# and the OnCreateSplashScreen method to do a much cleaner splash screen implementation.

JP Alioto
Thanks for the link to the info on the OnCreateSpashScreen method. I'll have to do some looking into that.
JimDel
+6  A: 

First of all, the way the splash screen on that page is done, using Thread.Abort, is not the right way to do things.

Never call Thread.Abort, unless you're in the process of shutting down the AppDomain the thread lives in.

Let me reiterate that for emphasis. The only time you should call Thread.Abort is when you know enough about Thread.Abort and how it behaves to know that you should never call it.

Take a look at this other question on StackOverflow: Multi-Threaded splash screen in c#?.


If you want to keep your existing solution, a possible better way would be to drop a timer into the splash screen form, set its timer to the time you want the splash screen to stay on screen, and call Close in its Tick event handler.

In the same venue, I would simply fire off that original thread, and remove the other lines.

In other words, from the first code block on that page, I would keep these two lines:

Thread th = new Thread(new ThreadStart(DoSplash));
th.Start();

Couple that with that timer on the form that makes the form self-closing, and you're in way better shape than trying to get to grips with Thread.Abort.

Which you should not call.

Lasse V. Karlsen
"The only time you should call..." Well put.
Michael Petrotta
I like the idea of using the "OnCreateSplashScreen" method because it seems to be the preferred way of doing it I see. But as a beginner there isn't a lot of explanation there to get me going. However, the answer given by "aku" on that same page you referenced did the trick for me. Thank you for pointing me in the right direction.
JimDel
Just found a nice site with a step by step for using the VB method. http://www.softwarepassion.com/?p=152
JimDel