[英]Why do exceptions in asynchronous C# socket code terminate my program without stack trace?
I have a C# Windows UI program, with a Window1 : Window
in whose constructor I start an asynchronous socket server that while (true) { /* BeginAccept */ }
and streams data to any client that connects using BeginSend
and EndSend
. 我有一个C#Windows UI程序,带有一个
Window1 : Window
在其构造函数中我启动一个异步套接字服务器while (true) { /* BeginAccept */ }
并将数据流式传输到使用BeginSend
和EndSend
连接的任何客户端。
If one of the clients is forcibly disconnected Socket.EndSend
throws an exception (a bit surprisingly ObjectDisposedException
instead of SocketException
, but whatever). 如果其中一个客户端被强行断开连接,
Socket.EndSend
会抛出异常(有点令人惊讶的是ObjectDisposedException
而不是SocketException
,但无论如何)。
That exception terminates my whole program (closing the window) but does not print a stack trace! 该异常终止了我的整个程序 (关闭窗口)但不打印堆栈跟踪!
Instead, I only get an innocent 相反,我只是一个无辜的人
A first chance exception of type 'System.ObjectDisposedException' occurred in System.dll
message in the Ouput panel. 输出面板中的消息。
If exceptions are silenced like this but still crash my program without stack trace, how am I supposed to find where the exception is thrown? 如果异常像这样被静音但仍然在没有堆栈跟踪的情况下崩溃我的程序,我怎么能找到引发异常的地方?
Well, it depends on what version of .NET you are using. 嗯,这取决于您使用的.NET版本。 But, that's generally what's documented will happen.
但是,这通常会记录下来会发生什么。
If you don't want that to happen, use top-level exception handlers in your thread entry points and "swallow" the exception--logging it or whatever else you want to do before exiting the thread. 如果您不希望这种情况发生,请在线程入口点中使用顶级异常处理程序并“吞下”异常 - 在退出线程之前记录它或您想要执行的任何其他操作。
In terms of methods like BeginSend
, the method given to BeginSend
(or BeginAccept
for that matter) is effectively the thread entry-point. 就像
BeginSend
的方法而言,给BeginSend
(或BeginAccept
)的方法实际上是线程入口点。 eg 例如
listener.BeginAcceptTcpClient(OnAccept, null);
//...
private static void OnAccept(IAsyncResult ar)
{
try {
var tcpClient = listener.EndAcceptTcpClient(ar);
//...
} catch(Exception ex)
{
Dump(ex);
return;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.