- Is writing zero bytes to a network stream a reliable way to detect closed connections?
- // stream is a Stream instance
- var abort = false;
- using (new Timer(x => {
- try
- {
- stream.Write(new byte[0], 0, 0);
- }
- catch (Exception)
- {
- abort = true;
- }
- }, null, 1000, 1000))
- {
- // Do expensive work here and check abort periodically
- }