- Home /
Using BackgroundWorker Class let Unity crash.
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.
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
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(m_MainThreadID)" "!Thread:EqualsCurrentThreadID(GetPersistentManager().GetMainThreadID())&& mode != kCreateObjectDontRegisterInstanceID" "m_ThreadCheck&&!Thread:EqualsCurrentThreadID(m_ThreadID)"
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. $$anonymous$$y 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: $$anonymous$$oved 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. :)
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!
Your answer
Follow this Question
Related Questions
Is it possible to query the Unity (3.0) transform tree from a secondary thread? 1 Answer
CompareBaseObjectsInternal can only be called from the main thread. 1 Answer
Multithreaded Input Detection 2 Answers
Unity Multithreading only works partial 0 Answers
Getting texture size off main thread 1 Answer