简体   繁体   中英

Why should I use BeginWrite instead of Write?

I am working on a service that manages up to 30.000 remote devices.

Doing some load tests it seems like the async method BeginWrite use more resources then regular Write (I think due to the overhead needed to create a callback thread); on the other hand, the Write method always returns faster then BeginWrite.

So my question is: what is the reason to prefer BeginWrite?

Another question is: any reason to set a WriteTimeout for sync Write? In which cases the Write method doesn't generate an immediate exception (due to closed socket, for example) but doesn't complete in a reasonable time?

EDIT : I have a set of worker threads (32 or 64), so I send data to 500-1000 devices at once and Write seems to work better than BeginWrite.

EDIT 2 : after a little search I understand that sync Write hangs only if the write buffer is full. Default write buffer size is 8KB, and I just send 1-2KB of data at once, so in my application Write should never hangs and it is performing better that BeginWrite. I set a security WriteTimeout of 1 second.

Scalability.

Synchronous calls block the thread while they complete. Async methods don't. Depending on how many concurrent requests you're going to be dealing with, this may matter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM