Trying to figure out whether to use async methods or not, for example:
and
unlike their synchronous versions of TcpListener.AcceptTcpClient
and NetworkStream.Read
. I looked at related topics, but I'm still a little unsure of one thing:
Question: The main advantage of using the asynchronous method is that the GUI is not blocked. However, these methods will be called separate threads of the Task , since this does not threaten this. In addition, TcpListener.AcceptTcpClient
blocks the thread until a connection is established, so there are no unnecessary processor cycles. Since this is so, why do so many recommend using asynchronous versions? It looks like in this case the synchronous versions will be superior?
In addition, another disadvantage of using asynchronous methods is the increased complexity and constant casting of objects. For example, you need to do the following:
private void SomeMethod() {
In contrast to this:
TcpClient client = listener.AcceptTcpClient();
It also seems that there will be much more overhead for asynchronous versions due to the need to create another thread. (Basically, each connection will have a stream, and then when reading this stream there will also be a different stream. Threadception!)
In addition, there is boxing and unboxing TcpListener and overhead associated with the creation, management and closure of these additional threads.
Basically, where there are usually separate streams for processing individual client connections, now there is an additional stream for each type of operation performed (reading / writing stream data and listening for new connections on the server)
Please correct me if I am wrong. I'm still new to streaming, and I'm trying to figure it all out. However, in this case, it seems like using conventional synchronous methods, and just blocking the thread would be the optimal solution?