The. NET Async Socket code is used in the project, and the following strange error occurs:
CompareBaseObjectsInternal can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
The strange reason is that the error does not affect program execution. Instead, it correctly receives the information returned by the server and displays the information correctly through the callback, indicating that the callback is executed. However, the second parser called from the callback is not executed. Is it because of the above error?
Baidu CompareBaseObjectsInternal search has only eight results. This is not the same as my situation. It is of little reference significance.
Then google, unfortunately, the old brother has been loading... So when I went to yahoo, there was no cramps. The previous articles found are on the unity Forum.
Http://forum.unity3d.com/threads/comparebaseobjectsinternal-error.184069/#post-1258700
Http://forum.unity3d.com/threads/comparebaseobjectsinternal-can-only-be-called-from-the-main-thread-annoying.195902/http://forum.unity3d.com/threads/comparebaseobjectsinternal-can-only-be-called-from-the-main-thread-annoying.195902/
Http://community.kii.com/t/error-comparebaseobjectsinternal-can-only-be-called-from-the-main-thread/378!
Http://answers.unity3d.com/questions/704284/comparebaseobjectsinternal-can-only-be-called-from-1.html
My problem was probably because I registered a callback in the main thread,
// Register the response code for all events
NetEventManager. GetInstance (). netMessageReceived_event + = OnNetMessageReceived;
NetEventManager. GetInstance (). gateServerConnected_event + = OnGateServerConnected;
NetEventManager. GetInstance (). errorMessageOccured_event + = OnErrorReceived;
Then the socketclient object is asynchronous, so multithreading is implemented. When the socketclient receives the message, it calls back the main thread function (inherited from monobehaviour), which leads to an error. The first blog post in the previous unity Forum clearly explains that unity imposes restrictions on the main API calling thread:
"Unity chose to limit API cballs to main-thread, to make a simple and solid threading model that everyone can understand use ."
According to this explanation, I comment out the above Code and the program will not report an error. It is estimated that the event response code should be placed in a non-monobehavior class to avoid the thread security issues mentioned above.
Since I am still a little bit familiar with multi-threaded programming, whether the above explanation is correct or not, is there any better solution? I hope you can give me some advice.