Unity3d报告奇怪的错误CompareBaseObjectsInternal can only be called from the main thread.
其中使用了该项目.NET的Async Socket代码。后来不知道什么时候这个奇怪的错误的出现:
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.
怪异是由于报错并没有影响程序运行,而是正确地接收了server返回的信息,而且通过回调正确显示出来。说明回调被运行了。可是从回调再调用一个解析器却一直不运行。是不是上面的错误导致的?
搜索百度CompareBaseObjectsInternal关键词仅仅有8个结果,http://blog.csdn.net/jixuguo/article/details/26693977相对有些意义,可是博文为转载。后来看到原文,大致的意思是假设从子线程调用主线程的callback。须要给回调加一个interface。
这个和我的情况不太一样,參考意义不大。
然后google。非常不幸古大哥一直载入载入。
。。
于是去yahoo,竟然还没有抽筋。
最前面搜索出来的几篇文章就是unity论坛上的。大致看出一些眉目。
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/
分析出来我的问题大概是在主线程里面注冊了回调,
//注冊全部事件的响应代码
NetEventManager.GetInstance().netMessageReceived_event += OnNetMessageReceived;
NetEventManager.GetInstance().gateServerConnected_event += OnGateServerConnected;
NetEventManager.GetInstance().errorMessageOccured_event += OnErrorReceived;
然后socketclient对象是异步的,所以做了多线程处理,当socketclient接收到消息时,回调了主线程的函数(继承自monobehaviour),这时候就导致了报错。上面第一篇unity论坛博文解释得比較清楚,大概就是unity对于API调用主线程做了限制:
“Unity chose to limit API calls to main-thread, to make a simple and solid threading model that everyone can understand and use.”
依据这个解释,我把上面的代码凝视掉。程序就不报错了。
预计这些事件响应代码要放到非monobehavior的类里面去。就能够避免上面说的线程安全问题。
因为我感兴趣的多线程编程的基本或白色,上面的解释是正确的、有没有更好的解决办法。也希望你有种。
版权声明:本文博主原创文章。博客,未经同意不得转载。
相关文章
- Web Api调用遇到错误提示System.Web.HttpException (0x80004005): The controller for path '' was not found or does not implement IController.
- golang错误:The process cannot access the file because it is being used by another process
- .Net Core 控制台程序错误:Can not find runtime target for framework '.NETCoreApp,Version=v1.0' compatible with one of the target runtimes: 'win10-x64, win81-x64, win8-x64, win7-x64'.
- win2008server R2 x64 部署.net core到IIS上出现【Failed to load the dll from [C:Program Filesdotnethostfxr1.0.1hostfxr.dll], HRESULT: 0x80070057】错误
- (转)HttpWebRequest以UTF-8编码写入内容时发生“Bytes to be written to the stream exceed the Content-Length bytes size specified.”错误
- visual studio 2013 for windows desk报error MSB8020: The build tools for v141错误
- Google Earth Engine —— 隐形错误get获取元素后结果无法筛选(字符串转数字函数)
- Eclipse正常SSM项目导入idea只能显示首页无法登录错误Failed to load resource: the server responded with a status of 404原因
- Jsp中无法使用EL表达式的解决方法错误Can not find the tag library descriptor for http://java.sun.com/jsp/jstl/core
- "多步操作产生错误,请检查每一步的状态值" 导致原因和排查过程
- Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
- The connection to the server ip:6443 was refused - did you specify the right host or port
- [转]ORA-01555错误总结(一)
- The last access date is not changed even after reading the file on Windows 7
- Dubbo错误No provider available for the service
- 前端错误监控,sentry入门配置详细教程
- hyper-v启动虚拟机时提示“The application encountered an error while attempting to change the state of the machine ‘虚拟机名称'”如何处理?
- The GuidRepresentation for the reader is CSharpLegacy, which requires the binary sub type to be Uuid
- The development prospect of SAP consultants in China in the next decade
- 《Python语言程序设计》——1.8 程序设计错误
- 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法!
- jsp头部报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- Layui使用笔记(二):tp5+layui实现分页 数据表单的使用 解决接口异常等错误
- Android Studio 常见错误 之 Servers certificate is not trusted 解决证书不可信任老弹出的 问题
- The incident LOST_EVENTS occured on the master. Message: error writing to the binary log, Error_code
- django:multivaluedictkeyerror错误
- The declared package does not match the expected package
- 若依前后端分离部署,出现 “无法访问系统资源 401”或者 “The request was rejected because the URL contained a potentially malicious String
- 影像工作站的数据库安装错误之Win7系统下pg服务无法启动