zoukankan      html  css  js  c++  java
  • Unity线程安全:CompareBaseObjectsInternal can only be called from the main thread

    在unity中我们使用多线程时。用子线程调用主线程时。用到unity的东西时就会报如下的错误。

    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.

    一个简单的办法就是。当多线程调用时。将内容展示存下来。然后通过主线程的函数去下发。比如Update下发

    例:

    public void BeginTheTimer()
    {
    	//建立连接
    	try
    	{
    		socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    		socketClient.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9070));
    		IsConnected = true;
    	}
    	catch (Exception e)
    	{ 
    		Debug.Log(e);
    	}
    	
    	Thread receiveThread = new Thread(new ThreadStart(socketReceive));
    	receiveThread.Start();
    	receiveThread.IsBackground = true;
    }
     
    //数据接收线程添加变量DateTime lastConnect=DateTime.now;
    //若接收数据为echo,则不作处理,若为其他数据,显示在richTextBox中;
    public void socketReceive()
    {
    	while (true)
    	{
    		try
    		{
    			byte[] buff = new byte[1024];
    			int count = socketClient.Receive(buff);
    			if (count > 0)
    			{
    				string str = Encoding.UTF8.GetString(bytes, 0, i);
                    message(str);
    			}
    		}
    		catch (SocketException)
    		{
    			IsConnected = false;
    			Thread.CurrentThread.Abort();
    		}
    	}
    }
    private void message(string data)
    {
    	notifierDataList.Add(data);
    }
     
    void Update()
    {
    	if(notifierDataList.Count > 0)
    	{
    		//如这。我用的是接口下发消息。通过字典来队列消息。每次下发一条。然后将已下发的移除
    		//就不会出现线程安全问题了
    		Notifier(999, null, notifierDataList[0]);
    		notifierDataList.RemoveAt(0);
    	}
    }
    

      转载来源:小宝个人笔记 » Unity线程安全:CompareBaseObjectsInternal can only be called from the main thread

  • 相关阅读:
    cocos2d-3.0 Helloworld::onTouchMoved的处理机制的推測
    一个Nodejs的简单计算測试程序
    js正則表達式语法
    奇妙的go语言(网页下载)
    java环境变量配置
    【数据结构与算法】二叉树深度遍历(非递归)
    $.each 和$(selector).each()的差别
    HDU-2844-Coins(多重背包)
    curl命令具体解释
    Arduino 数码管LED屏驱动
  • 原文地址:https://www.cnblogs.com/UnrealEra/p/6141118.html
Copyright © 2011-2022 走看看