Question Beginner using multithreading and streamreader - thread hangs and become unresponsive

Tset_Tsyung

New member
Joined
May 8, 2016
Messages
2
Programming Experience
Beginner
Hi all,

First post here - XD

So I'm working on a project in Unity (an interactive, live animated twitchbot). I'm using TcpClient and StreamReader to connect to the twitch chat and read the messages.

Of course streamreader.readline() causes the program to completely hang whilst waiting for message. As this is an animated project this is no good. Therefore I've start looking into threading :D

Problem is that whilst there are message on the stream it runs fine... but as soon as the stream is empty the thread becomes completely unresponsive. Even when a new message comes through it does nothing with it. At least when it wasn't threaded it would eventually continue when a message did come through.

Here's my code
C#:
private void ClientReadStream()
    {
        while (true) {
            Debug.Log ("Checking for message");
            string message = inputStream.ReadLine ();
            Debug.Log ("We've recieved a message of " + message);
            MessageInfo latestMessage = new MessageInfo ();
            latestMessage.message = message;
            //latestMessage.timeReceived = Time.time;


            messageList.Add (latestMessage);
        }
    }

Just to restate, once the initial list of message from the twitch servers is finished the thread just ceases up, even when new messages come through it doesn't respond to them. I've tried it with and without a receivedTimeout paramet. What am I missing? I should state that I only started looking into threading yesterday, but with the little knowledge I have I don't know where to begin...

Any advice or help is greatly appreciated.

Mike

P.S. just as an FYI it stops producing the "Checking for message" debug entry as well... :(
 
[sigh] Nevermind...

It was the timeout variable. It seems that even though I had removed it I hadn't actually recompiled and tested it... I really should have some coffee and food before coding in the mornings, lol.

Thanks anyway all!
 

Latest posts

Back
Top Bottom