Visual Studio .NET - Issue regarding splash screen closing.

Asked By Prajakta A on 19-Nov-09 01:17 AM

I have created a splash screen code in C#. When I run the application in run without debug mode the splash screen overlaps my login form instead of closing. but this is not the case in debug mode.

 

public LoginForm()

{

InitializeComponent();



// Code to show splash screen.


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

//th.ApartmentState = ApartmentState.STA;

 

//th.IsBackground = true;

th.Start();


Thread.Sleep(1000);

//this.Focus();

th.Abort();


Thread.Sleep(1000);

}


private

void DoSplash()

{


SplashScreen sp = new SplashScreen();

sp.ShowDialog();


}


This code is in my LoginForm.cs file.


Can anyone help me out with this ?

Thank you.

You can check this out. - Michael Detras replied to Prajakta A on 19-Nov-09 02:32 AM

I've found a link that might be helpful to you.

http://www.codeproject.com/KB/dialog/DGDevSplashScreen.aspx

It shows the splash screen before creating the main form, I think. I believe the design is much cleaner. Also, I would stay away from using multiple threads and sleep if possible.

Use MethodInvoker delegate - Huggy Bear replied to Prajakta A on 19-Nov-09 02:50 AM

For invoking the SplashForm use the MethodInvoker delegate (nothing but a built in delegate). While closing the SplashForm you got to use the InvokeRequired flag because it helps in avoiding the cross thread access issue on the SpashForm. Please modify your code to.

public Form1()
        {
            InitializeComponent();

            //Do the splash
            DoSplash();
            Thread.Sleep(5000);
            //Close the splash
            CloseSplash();
            Application.DoEvents();
        }

        private void DoSplash()
        {
            if (_splashForm != null)
                return;
            //Built in delegate
            MethodInvoker methodInvoker = new MethodInvoker(ShowSplashScreen);
            methodInvoker.BeginInvoke(null, null);//Invokes asynchronously
        }

 public void ShowSplashScreen()
        {
            _splashForm = new SplashForm();
            Application.Run(_splashForm);
        }

        private void CloseSplash()
        {
            if (_splashForm == null)
                return;

            //Use this flag in order to avoid the cross thread access of _splashform
            if (_splashForm.InvokeRequired)
            {
                //Built in delegate
                MethodInvoker closeMethodInvoker = new MethodInvoker(CloseSplash);
                _splashForm.Invoke(closeMethodInvoker);
            }
            else
            {
                _splashForm.Close();
                _splashForm = null;
            }
        }

Tested both in Release and Debug mode and couldn't find any glitches.
       

Thank you. - Prajakta A replied to Huggy Bear on 19-Nov-09 06:05 AM


Your Solution proved to be great help.