Load content on background thread

Hello,

I have some question about loading content on background thread. Lets start with a piece of code:

        protected override void LoadContent()
        {
...
            SceneManager.LoadScenesContent(Content);

            Thread t = new Thread(() => LoadContentOnBackgroundThread()) { IsBackground = true, Name = "LoadContentThread" };
            t.Start();
            t.Join();
        }


        private void LoadContentOnBackgroundThread()
        {
            AudioManager.LoadContent(Content);
        }

I try to load assets like textures / fonts on main thread but audio on background thread. It works fine. When I move loading of textures / fonts also into background thread it stops working. I mean like debugger stops on loading some texture from content manager. How to load content on background thread properly ? I really have no idea what causes this issue, looks like content manager stucks on loading of texture, so maybe it is locked somehow ?

Offtopic: I really need loading of content on background thread because on Android, during splash screen, I have black screen instead of my splash graphic (reproduce rate about 50%). So sometimes it works fine, sometimes I have black screen.

I set up my splash screen “regular” way, just with style:

<style name="Theme.Splash" parent="android:Theme">
    <item name="android:windowBackground">@drawable/splash_background</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
  </style>

the problem you have is that we only have one GL Context on Android. So when you load content on a background thread we still need to Create the actual texture on the main UI thread.

So your problem is the t.Join(); call. It will block the UI thread, but the UI kinda needs to not be blocked since the background thread will be calling into it to create the textures.

My advice perhaps is to load the minimal amount of texture you need to get a “loading” screen or something showing when load the remaining stuff once the main Update loop is running.

Alternatively switch to using a Task. Since that can have a continuation event 'ContinueWith` which you can add an event to set some kind of flag which will tell you the resources are loaded.