Search code examples
c#splash-screen

Splash Screen that doesn't go away


I'm using a Splash Screen from Here. I love how simple it is. But the problem with it is that the splash 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();
}

Solution

  • 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.