Note that there are some explanatory texts on larger screens.

plurals
  1. POTcpClient and StreamReader blocks on Read
    primarykey
    data
    text
    <p>Here's my situation:</p> <p>I'm writing a chat client to connect to a chat server. I create the connection using a TcpClient and get a NetworkStream object from it. I use a StreamReader and StreamWriter to read and write data back and forth. </p> <p>Here's what my read looks like:</p> <pre><code>public string Read() { StringBuilder sb = new StringBuilder(); try { int tmp; while (true) { tmp = StreamReader.Read(); if (tmp == 0) break; else sb.Append((char)tmp); Thread.Sleep(1); } } catch (Exception ex) { // log exception } return sb.ToString(); } </code></pre> <p>That works fine and dandy. In my main program I create a thread that continually calls this Read method to see if there is data. An example is below.</p> <pre><code>private void Listen() { try { while (IsShuttingDown == false) { string data = Read(); if (!string.IsNullOrEmpty(data)) { // do stuff } } } catch (ThreadInterruptedException ex) { // log it } } ... Thread listenThread = new Thread(new ThreadStart(Listen)); listenThread.Start(); </code></pre> <p>This works just fine. The problem comes when I want to shut down the application. I receive a shut down command from the UI, and tell the listening thread to stop listening (that is, stop calling this read function). I call Join and wait for this child thread to stop running. Like so:</p> <pre><code>// tell the thread to stop listening and wait for a sec IsShuttingDown = true; Thread.Sleep(TimeSpan.FromSeconds(1.00)); // if we've reach here and the thread is still alive // interrupt it and tell it to quit if (listenThread.IsAlive) listenThread.Interrupt(); // wait until thread is done listenThread.Join(); </code></pre> <p>The problem is it never stops running! I stepped into the code and the listening thread is blocking because the Read() method is blocking. Read() just sits there and doesn't return. Hence, the thread never gets a chance to sleep that 1 millisecond and then get interrupted.</p> <p>I'm sure if I let it sit long enough I'd get another packet and get a chance for the thread to sleep (if it's an active chatroom or a get a ping from the server). But I don't want to depend on that. If the user says shut down I want to shut it down!!</p> <p>One alternative I found is to use the <a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.dataavailable%28VS.71%29.aspx" rel="nofollow noreferrer">DataAvailable</a> method of NetworkStream so that I could check it before I called StreamReader.Read(). This didn't work because it was undependable and I lost data when reading from packets from the server. (Because of that I wasn't able to login correctly, etc, etc)</p> <p>Any ideas on how to shutdown this thread gracefully? I'd hate to call Abort() on the listening thread...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload