1

I have shown a splash screen it works fine, but the main form will not shown on task bar and focus.

There are 2 form 1. SplashScreen 2. formMain

On splashscreen I added a timer and do some code below.

public SplashScreen()
        {
            InitializeComponent();
        }

        private void splashtimer_Tick(object sender, EventArgs e)
        {
            progressBar1.Increment(1);
            if (progressBar1.Value == 100) splashtimer.Stop();
        }

On the main form "formMain"

I added the code below.

public formMain()
    {
        this.ShowInTaskbar = true;
        Thread t = new Thread(new ThreadStart(ShowSplashScreen));
        t.Start();
        Thread.Sleep(5000);
        InitializeComponent();
        t.Abort();


    }

    public void ShowSplashScreen()
    {
        Application.Run(new SplashScreen());            
    }

Splash screen works fine but the main form will not focus. I run EXE from Debug folder and run it, splash screen displayed and main form not sown. Taskbar icon not shown. When in Ctrl+tab the formMain is shown. Why???

Where is the problem?

Anil P
  • 33
  • 7
  • Never ever call `Thread.Abort()` **unless** you are trying to forcibly shut down your entire app. It can leave the process in an undefined state. – Enigmativity Jan 21 '16 at 03:19
  • I i comment abort() it will not hide the splash screen – Anil P Jan 21 '16 at 04:52
  • Yes, but it could kill your entire app. You should keep a reference to `ss = new SplashScreen()` and call `ss.Invoke((Action)(() => ss.Close()));` instead. – Enigmativity Jan 21 '16 at 05:26
  • You need to display your main window *before* you close the splash screen. The way you are doing it now, there is a brief moment while your app has no window that can get the focus. Windows will find another one to give the focus, it will be one owned by another app. Your main window finally shows up, behind it. Just don't do this yourself, use the splash screen support [built into the .NET Framework](http://stackoverflow.com/a/393870/17034). – Hans Passant Jan 22 '16 at 22:16

2 Answers2

0

Try this code for displaying a splash screen:

    private void formMain_Load(object sender, EventArgs e)
    {
        EventHandler activated = null;
        activated = (s2, e2) =>
        {
            this.Activated -= activated;
            this.ShowInTaskbar = true;
            var splash = new SplashScreen();
            splash.ShowInTaskbar = false;
            splash.Show();
            var timer = new System.Windows.Forms.Timer();
            EventHandler tick = null;
            tick = (s3, e3) =>
            {
                timer.Enabled = false;
                timer.Tick -= tick;
                timer.Dispose();
                splash.Close();
                splash.Dispose();
            };
            timer.Tick += tick;
            timer.Interval = 5000;
            timer.Enabled = true;
        };
        this.Activated += activated;
    }
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • I tried this code, it works but the splash screen shown inside the formMain. I need first splash screen then formMain. – Anil P Jan 21 '16 at 04:48
0

Splash screen works fine but the main form will not focus. I run EXE from Debug folder and run it, splash screen displayed and main form not sown. Taskbar icon not shown. When in Ctrl+tab the formMain is shown. Why???

I tried on VS 2015, and my formMain and SplashScreen worked fine, except as you noted, formMain was not focussed. That probably is since you use Application.Run to get focus to SplashScreen after formMain was created and focussed.

In either case, your approach is not so clean. Try the way I have it below. This will fix the focus issue too

public partial class SplashScreen : Form {

    public SplashScreen() {
        InitializeComponent();
        progressBar1.Style = ProgressBarStyle.Marquee;
    }

}

public partial class formMain : Form {
    public formMain(Form splash) {
        InitializeComponent();

        // make sure to keep yielding to GUI updates, else your progressbar will nto refresh
        for (int i = 0; i < 100; ++i) {
            Thread.Sleep(100); // do some work
            Application.DoEvents();
        }

        splash.Close();

    }
}

static class Program {
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        var frm = new SplashScreen();
        frm.Show();

        Application.Run(new formMain(frm));

    }
}
Vikhram
  • 4,294
  • 1
  • 20
  • 32