Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UI Thread is Blocked when showing Splash Screen [duplicate]

Possible Duplicate:
C# Splash Screen Problem

I am new to c# , i am working on splash screen which runs when the software starts.. I have a function in splash Screen class which checks the database. I am using thread to call the function

        sc = new splashScreen();

        checkDLLThread = new Thread(new ThreadStart(sc.checkDLLS).BeginInvoke);
        checkDLLThread.Start();

        while (checkDLLThread.IsAlive)
        {
            Thread.Sleep(200);
        }

Problem is UI is blocked until the thread is Alive. And in final it give me database connection status message. Here is my code. I have used checkDLLThread.join() but it also doesnt work.

like image 365
greatmajestics Avatar asked Jan 27 '26 18:01

greatmajestics


2 Answers

A splashscreen is simply an image to 'amuse' the user while your app is loading. Use the app_load method to execute code on startup:

Like so: (in app.xaml and app.xaml.cs)

    <Application /some references to stuff/ Startup="Application_Startup" >

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        // your startupcode
    }

Also, I think the BackGroundworker class is better for things like this, if you don't want to bother the UI.

like image 129
AmazingDreams Avatar answered Jan 30 '26 08:01

AmazingDreams


Unblocking the UI thread requires returning from your event handler. There is no alternative to doing that. Here is some pseudo-code:

OnStartup:
 Start new Thread
 Disable UI
 Show Splash Sceen
 Return!

Thread:
 Check Database
 if (not ok)
  Show Message box
  Exit Application
 else
  Enable UI
  Remove Splash Screen

The trick is to let the thread show the message and so one once it is done. Don't wait for the thread, let the thread do it itself.

Your thread probably needs to use the Invoke function to access the UI. You might wan't to read about that a bit because there is no way around it if you want to do asynchronous work on a background thread.

like image 42
usr Avatar answered Jan 30 '26 08:01

usr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!