A couple (err..) of questions about TcpClient (C#)
I am a bit confused about certain aspects of [TcpClient][1]

1. My server form process does not end when I close the form for some reason. The two threads I created are no longer running when I close the form and to be sure I tried aborted the threads too and the process still won't die until I explicitly kill it. Not sure what's going on there..

2. Is there a better way to listen for new clients or new messages than just enclosing `TcpListener.AcceptTcpClient()` and `TcpClient.GetStream.Read()` in `while (true)` loops? I thought C# was an event-driven language. This type of thing seems so messy and inefficient. What if no messages or new clients arrive for hours? That entire time is just wasted CPU cycles..

3. When should I be using `using` statements? I see so many different code examples, some with 4 nested using blocks and others with none at all.

4. How should I choose a buffer length? Some examples use 1024, some 2048, and some 4096. Seems rather arbitrary?

5. Having to constantly create invokers and use reflection just to update controls is rather cumbersome, tedious, and inefficient it seems. Is there not an alternative to constantly doing something like [this?][2] (sorry, embedding the code in this list is not working for me) (Using .NET 4.0).

6. What is the best way to store a list of connected clients on the server? I feel like a `Dictionary` would be optimal but I'm not sure what to use as the key value pair. Perhaps the IP address as the key and the actual TcpClient object as the value?

7. When should I be using Async methods in my code? I notice there's Async versions of a lot of methods.. For example, `TcpClient.GetStream().Read` or `TcpClient.GetStream().BeginRead() and TcpClient.GetStream().EndRead()`?

8. Creating a `ParameterizedThreadStart` thread forces the argument to be of type object. Thus, when passing the TcpClient to the method, wouldn't it need to be boxed and then unboxed? Currently at the beginning of my method I have to cast the object back to a TcpClient. Is there not a generic solution to this?

Eek, sorry about all the questions. Please don't feel like you need to answer all of them. I am just a bit confused and want to make sure to be following best practices. I've been trying to find good code examples but so many people have such different approaches to the design of a simple client/server program!

Either way, thanks for reading!

  [1]: http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx
  [2]: http://pastebin.com/xJv5R6dL
A couple (err..) of questions about TcpClient (C#)
===

I am a bit confused about certain aspects of [TcpClient][1]

1. My server form process does not end when I close the form for some reason. The two threads I created are no longer running when I close the form and to be sure I tried aborted the threads too and the process still won't die until I explicitly kill it. Not sure what's going on there..

2. Is there a better way to listen for new clients or new messages than just enclosing `TcpListener.AcceptTcpClient()` and `TcpClient.GetStream.Read()` in `while (true)` loops? I thought C# was an event-driven language. This type of thing seems so messy and inefficient. What if no messages or new clients arrive for hours? That entire time is just wasted CPU cycles..

3. When should I be using `using` statements? I see so many different code examples, some with 4 nested using blocks and others with none at all.

4. How should I choose a buffer length? Some examples use 1024, some 2048, and some 4096. Seems rather arbitrary?

5. Having to constantly create invokers and use reflection just to update controls is rather cumbersome, tedious, and inefficient it seems. Is there not an alternative to constantly doing something like [this?][2] (sorry, embedding the code in this list is not working for me) (Using .NET 4.0).

6. What is the best way to store a list of connected clients on the server? I feel like a `Dictionary` would be optimal but I'm not sure what to use as the key value pair. Perhaps the IP address as the key and the actual TcpClient object as the value?

7. When should I be using Async methods in my code? I notice there's Async versions of a lot of methods.. For example, `TcpClient.GetStream().Read` or `TcpClient.GetStream().BeginRead() and TcpClient.GetStream().EndRead()`?

8. Creating a `ParameterizedThreadStart` thread forces the argument to be of type object. Thus, when passing the TcpClient to the method, wouldn't it need to be boxed and then unboxed? Currently at the beginning of my method I have to cast the object back to a TcpClient. Is there not a generic solution to this?

Eek, sorry about all the questions. Please don't feel like you need to answer all of them. I am just a bit confused and want to make sure to be following best practices. I've been trying to find good code examples but so many people have such different approaches to the design of a simple client/server program!

Either way, thanks for reading!

  [1]: http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx
  [2]: http://pastebin.com/xJv5R6dL