- Home /
NetworkStream.GetStream() Coroutine
how can i make NetworkStream.GetStream a coroutine?
i currently use Threading to avoid the main thread because the NetworkStream.GetStream waits for data before continuing and that temporarily freezes Unity until new data arrives. so i have no idea how to make this a coroutine
I'm doing a whole workaround to get data from my thread to the main thread and that just slows things down
Answer by MadDave · Sep 04, 2012 at 01:07 PM
You cannot and should not.
Coroutines are a form of cooperative multithreading that are actually run on the main thread. Since you cannot interrupt NetworkStream.GetStream() it cannot be a coroutine. And you certainly don't want your network I/O to block the main thread.
Network I/O definitively belongs in a separate worker thread.
thank you! i guess its just hoping that Unity will soon support Threading so i can acces the main thread directly from my thread
Unity does support threading. You're obviously doing it. Communication between threads is always a bit complicated and maybe Unity could do a little bit more to help but... see one of my other answers for more insights: http://answers.unity3d.com/questions/305882/how-do-i-invoke-functions-on-the-main-thread.html