Unity 无法退出播放器 mod (ZMQ)

Unity can't quit player mod (ZMQ)

对不起我的英语

我用 while(true) 启动一个新线程以从套接字接收信息,但是当我想退出时我必须使用 Windows 任务管理器。事实上,永远不会到达 OnApplicationQuit() 方法并且 Unity 冻结。

我想知道是否有办法知道是否有人试图戒烟或其他方法。

提前致谢

PS : 我已经看过了 post : https://github.com/zeromq/netmq/issues/526

方法开始:

void Start()
    {
        stopThread = false;
 
        //thread that allow the while(true) in Unity
        thread = new Thread(connectAndReceive);
        thread.IsBackground = true;
        thread.Start();
    }

接收信息的方法:

 /**
         * Create the router socket and receive informations from onvif server
         */
        void connectAndReceive()
        {
            AsyncIO.ForceDotNet.Force();
            zmqPort = Config.GetZMQPort();
            string socketPort = String.Concat("tcp://127.0.0.1:", zmqPort);
     
            using (var server = new RouterSocket(socketPort))
            using (var poller = new NetMQPoller())
            {
                poller.RunAsync();
                Debug.Log("Listening…");
               
                while (!stopThread)
                {
                    //Give the message send by client[identity, message]
                    //var clientMessage = server.ReceiveMultipartMessage();        
                    try
                    {              
                         NetMQMessage clientMessage = new NetMQMessage();
                         var timeout = new System.TimeSpan(0, 0, 20); //1sec
     
                         bool isConnected = server.TryReceiveMultipartMessage(timeout, ref clientMessage);
     
                         //message = message send by Onvif server
                         message  = clientMessage[1].ConvertToString();
                         identity = clientMessage[0].ConvertToString();
     
                         Debug.Log("Message : " + message + ", From : " + identity);
                    }
                    catch (Exception e)
                    {
                         //Nothing received
                    }      
                }
               
            }
        }

方法退出:

 void OnApplicationQuit()
        {
            UnityEngine.Debug.Log("Try to QUIT");
            stopThread = true;
            thread.Join();
            NetMQConfig.Cleanup(true);
        }

我只是替换:var timeout = new System.TimeSpan(0, 0, 20); 通过 var timeout = new System.TimeSpan(0, 0, 0);

现在好了