C# splash screen used while loading data

I have an application that sets up a .dll with data when it starts up.
While this is loading I display a splash screen which is created using a ThreadWorker class.
This works fine unless the user clicks on another application while it is showing, in which case it locks up.
How can I keep this showing normally.

public void StartSplashScreenThread()
        {
            splashScreenWorker = new SplashScreenWorker(FDataCenterVersion);
            splashScreenWorkerThread = new Thread(splashScreenWorker.DoWork);
            splashScreenWorkerThread.Priority = ThreadPriority.Normal;
            splashScreenWorkerThread.IsBackground = true;
            splashScreenWorkerThread.Start();
        }


class SplashScreenWorker : ThreadWorker
    {
        SplashScreen splashScreen;
        String dataCenterVersion;
        public SplashScreenWorker(String aDataCenterVersion)
        {
            dataCenterVersion = aDataCenterVersion;     
        }


        public override void DoWork()
        {
            try
            {
                splashScreen = new SplashScreen(dataCenterVersion);
                splashScreen.IncreaseProgress();
                splashScreen.Show();

                while (!Stopping)
                {
                    try
                    {
                        splashScreen.IncreaseProgress();
                    }
                    catch (Exception e)
                    {
                        
             
                    }
                    Thread.Sleep(200);
                }
                

            }
            finally
            {
                splashScreen.Hide();
                splashScreen.Close();
                SetStopped();
            }
        }
    
    }

public partial class SplashScreen : Form
    {
        String dataCenterVersion;
        public SplashScreen(String aDataCenterVersion)
        {
            InitializeComponent();
            dataCenterVersion = aDataCenterVersion;
            Text = "Loading Data Center " + dataCenterVersion + " Please Wait...";
        }

        public void IncreaseProgress()
        {
            if (PB_SplashScreen.Value >= PB_SplashScreen.Maximum)
            {
                PB_SplashScreen.Value = 0;
            }
            PB_SplashScreen.Increment(1);
        }
    }

Open in new window

LVL 1
jetbetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael FowlerSolutions ConsultantCommented:
Try displaying the splash screen on the UI thread and use another thread to load the dll

Michael
0
Jacques Bourgeois (James Burger)PresidentCommented:
The Show method is asynchrone, it does not prevent the following code from running, you do not need to start it on another thread.

Simply call Show on your splashcreen, setup your dll and then close the form.

splashScreen.Show();
// Do your stuff
splashScreen.Close();

If loading the dll is too short, simply add a Timer in the splashScreen form and let it close the Form when it expires.
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
The article at link:

Automatic closing of popup window

explains way for showing splash screen while some long work is going on main thread. And it then closes the splash screen as work get finished.
0
jetbetAuthor Commented:
"Try displaying the splash screen on the UI thread and use another thread to load the dll

Michael "

I have tried this solution but although I pass the object to be initialized as a ref and can see it being set inside the thread, when the thread completes the object is set to null

public void StartDataStoreReloadThread()
        {

            dataStoreUpdaterWorker = new DataStoreUpdateWorker(ref dataCenter, ref currentAddress, ref currentPort);
            dataStoreUpdateThread = new Thread(dataStoreUpdaterWorker.DoWork);
            dataStoreUpdateThread.Priority = ThreadPriority.Normal;
            dataStoreUpdateThread.IsBackground = true;
            dataStoreUpdateThread.Start();
        }

Open in new window


public DataStoreUpdateWorker(ref NotifiedDataStore aDataStore, ref String aCurrentAddress, ref String aCurrentPort)
        {
           dataStore = aDataStore;  
           currentAddress = aCurrentAddress;
           currentPort = aCurrentPort;
        }

Open in new window


try
                {
                    dataStore = null;
                    dataStore = new NotifiedDataStore(channel,
                                                       primaryIP,
                                                       primaryPort,
                                                       secondaryIP,
                                                       secondaryPort,
                                                       system,
                                                       systemID,
                                                       heartbeat,
                                                       timeout,
                                                       true,
                                                      url,
                                                      datastoreTimeout,
                                                      false);
                }

Open in new window

0
Jacques Bourgeois (James Burger)PresidentCommented:
Instead of trying to complicat things with threads, have you tried my simple solution:

splashScreen.Show();
// Do your stuff - No need to start another thread. The form is visible on the screen, and since
// Show is asynchrone (not blocking), the code between the Show and the Close runs while the form is displayed.
splashScreen.Close();
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jetbetAuthor Commented:
I tried that but could not get the progress bar working.

I have looked at the other solutions as well, but some seem far more complicated than I need.

I would like to be able to call a full reload (with splash) at any stage so putting it in the app start up also is not appropriate.

All I need is for a modal screen to come up to stop the user interacting with the form while I reload the data.

If I do not get something working soon, I will accept your solution and forget about the progress bar.
0
jetbetAuthor Commented:
Thanks for the help
0
Jacques Bourgeois (James Burger)PresidentCommented:
You never talked of the the ProgressBar in the discussion. It showed only in your code. So I did not consider it as an issue.

You might try to call Refresh on the splashscreen form after calling your IncreaseProgress method. This is sometimes needed for a form to react after changing something from outside the form itself.

Or better, if you are OK with running the application with visual styles enabled (Application.EnableVisualStyles()), then simply set the ProgressBar.Style property to Marquee and you will automatically have a ProgressBar that behaves like what you are trying to achieve. Enabling visual styles should be done before you display your form, and there is no way that I know to disable it afterward.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.