Search code examples
c#unity-game-enginezeromqnetmq

Unity can't quit player mod (ZMQ)


sorry for my english

I start a new thread with a while(true) to receive information from socket but when I want to quit I have to use the Windows task manager. In fact, the method OnApplicationQuit() is never reached and Unity freeze.

I would like to know if there a way to know if someone try to quit or other method.

Thank you in advance

PS : I've already seen this post : https://github.com/zeromq/netmq/issues/526

The method start :

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

Method to receive information :

 /**
         * 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
                    }      
                }
               
            }
        }

And the methode quit :

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

Solution

  • I just replace : var timeout = new System.TimeSpan(0, 0, 20); by var timeout = new System.TimeSpan(0, 0, 0);

    and now it's ok