Creating a thread and running a task in it is trivial in C#, you can do it in 3 lines of code
ThreadStart childref = new ThreadStart(CallToChildThread);
Thread childThread = new Thread(childref);
childThread.Start();
The difficult bit is making the rest of your code thread safe.
You have to think t through properly.
In your case you would want to create a Room class and put everything your game needs in this.
You have two of them, one the game is using , and one that is being loaded in the background
Only when the loading thread has completed shuld you touch the new room
Otherwise all sorts of nasty things can happen.
This is probably more information than you need, but it illlustrates what I am talking about.
I had a lot of problems on one game that showed up as a crash in a random class when a level is being torn down. The specific symptom was that the “this” pointer was null.
Now there will be people out there right now screaming at their monitors saying “that’s not possible”.
In fact the latest versions of C++ have actually made it illegal to check for a null this pointer as in their tiny minds it is totally impossible.
The problem was I had multiple classes being disposed of that held pointers to other classes that were also being disposed of. The rules said “if you hold a pointer to a class , you must null it when you are disposed”, but lazy coders often forgot. Since they are referenced counted, the pointer hung around even when the object had been disposed, and since this was happening across multiple threads, update could be called on a null object.
The only way I could solve these was to modify the system memory allocator to hold the stack trace when a new object was created so I could look through the memory allocations to find which object was hanging on to a refernce.
As you can imagine this was not fun, slow , painful , and my retribution on the offending coder was biblical.
So think hard before you decide to step into the dark side Luke… errrr Mando…
![:smiley: :smiley:](https://community.monogame.net/images/emoji/twitter/smiley.png?v=9)