I try to implement a BackgroundWorker for my Database, so that my applikation does not freeze whenever i try to get huge amount of data out of it.
All things which happening in my BackgroundWorker is only the Database select execution.
I save my results in a List.
At the BackgroundWorker.completed Event i start creating gameobjects based on the database entrys, and than Unity crashes. Always with the error messages:
"!Thread:EqualsCurrentThreadID(GetPersistentManager().GetMainThreadID())&& mode != kCreateObjectDontRegisterInstanceID"
Is it possible to multithread in Unity3d? And if. Do you have a hint where my mistake could be?
Answer by Mike 3
Jul 06, 2010 at 05:32 PM
You can't access most of the unity API from threads, it'll just blow up like you've seen
What you need to do is buffer the creation requests into an array or list, then read them from Update
This goes for moving objects (via physics or transform), creating components and gameobjects, textures, and many other things - basically everything extending from UnityEngine.Object (And some which don't on top of that)
I did this already. My problem only was that when i made my database.command execution. And sometimes i asked for kinda 2k and more entrys, unity freezed until the whole request was done. I only wanted this done in a seperate thread. What i did now was: Moved all the GameObject creation code outside from the thread complete Handler. Than, in a seperate update function i check if the backgroundworker is not busy, and if there are new dbResults. If so it starts creating the Gameobjects (one per frame), which now works fine and fast. :)
Yup yup, that's similar to how I do it too :)
can that method work on iOS? Has anyone tested in in Unity3.x on iOS?
Answer by Oksana Iashchuk
Sep 26, 2012 at 02:07 PM
Cool, you could lock here for multi-threading http://u3d.as/content/orange-tree/thread-queue
it's a part of http://u3d.as/content/orange-tree/sqlite-kit which actualy already has asynchronous sqlite database processing.
good example: http://forum.unity3d.com/threads/152557-Meet-new-easy-thread-queue-asset!
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
1 Person is following this question.
Is it possible to query the Unity (3.0) transform tree from a secondary thread?
CompareBaseObjectsInternal can only be called from the main thread.
Multithreaded Input Detection
Loading large maps - 30000x30000 pixels (threading / parallel loading)
GPU Multithreading in Unity 3D 5 [NVIDIA TESLA K80]