Tell me what the problem may be - I compile the same project in Debug mode on different computers and in one case the exception intercepted by me is consistently thrown out, but in the other case it isn’t.

The code is as follows: a socket is created, asynchronously connected to the server and starts receiving data from it. If there is no response from the server, then in the continuation of the task of receiving data from the server, I put a stub, which logs the error.

//Подключение: socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); public async Task ConnectAsync(IPEndPoint remoteIpEp) { await Task.Factory.FromAsync(socket.BeginConnect(remoteIpEp, null, null), socket.EndConnect).ConfigureAwait(false); } //Чтение строки: public async Task<string> ReadLineAsync() { using (NetworkStream ns = new NetworkStream(socket)) { using (StreamReader sr = new StreamReader(ns, Encoding.UTF8)) { return await sr.ReadLineAsync(); } } } //Чтение строки с таймаутом ожидания ответа public async Task<string> ReadLineAsync(TimeSpan timeout) { Task<string> rxTask = ReadLineAsync(); if (await Task.WhenAny(rxTask, Task.Delay(timeout)).ConfigureAwait(false) != rxTask) { rxTask.Forget(); socket.Close(); throw new TimeoutException(); } return await rxTask; } public static void Forget(this Task task) { task.ContinueWith(t => { if (t.Exception != null) { Console.WriteLine($"Забытая задача завершена с ошибкой: {task.Exception}\r\n"); } }); } 

The connection to the server is established, but the server does not send any data (especially to initiate a timeout waiting for a response from the client). On one computer, the timeout is processed correctly; in the Output studio window, I see an exception message.

If I compile the same project on another computer, then the ObjectDisposedException exception in System.Net.Sockets.Socket.EndReceive (IAsyncResult asyncResult, SocketError & errorCode) consistently pops up: enter image description here

However, I can continue working and the application will not close, the Forget (..) method log will be displayed in the Output window. If I launch a project without a debugger and outside the studio, there will not be any errors either.

Why does the studio throw this exception?

  • Thank you, Gennady! Correctly, I understand that these settings are used only for debugging, so as to force throwing exceptions of the specified type? For what purposes it may be needed? - swd
  • @GennadiP worth making out your comments as an answer. - rdorn pm

1 answer 1

Check the box next to Break when this exception type is thrown uncheck. On the computer where it does not stop, the stop on this exception is most likely disabled. All exceptions settings are Debug -> Exceptions .