- Home /
My Coroutine is freezing my game? (no loops, serverSocket.Connect)
I made a coroutine for a login process with the understanding that if there was a wait it would not block the thread and freeze the game.
Here is a partial of the code:
IEnumerator LoginProcess()
{
bl_LoginManager.UpdateDescription("Logging In...");
bl_LoginManager.LoadingCache.ChangeText("Submitting Login Request...", true);
bl_LoginManager.LoadingCache.ChangeText("Waiting for Response", true, 1.2f);
//Begin Server Connection
Debug.Log("Connecting");
TcpClient serverSocket = new TcpClient();
serverSocket.Connect("222.222.222.222", 8888); //Fake IP to test bad connection
}
The game freezes on serverSocket.Connect
even though it's in a coroutine, what causes this, how do I go about fixing this?
Answer by Destructicorn · May 09, 2015 at 09:56 PM
TCPClient.connect is a blocking call. Which means it stops the main thread from running until it either connects or fails. Coroutines still run on the main thread, I don't remember exactly when they run, either before or after update. If you don't want the connect call to freeze your whole program you should break it off into a separate thread.
Your answer
Follow this Question
Related Questions
Hold or Wait while Coroutine finishes 6 Answers
Help with waiting for input? I'm too used to how Console.ReadLine() works. 0 Answers
Coroutine called in different instances 2 Answers
WaitForSeconds Question 2 Answers
Unity Keeps Freezing on Play 5 Answers