带有TaskFactory.FromAsync的Socket.EndAccept上的System.AggregateException(System.AggregateException on Soc

编程入门 行业动态 更新时间:2024-10-28 01:13:01
带有TaskFactory.FromAsync的Socket.EndAccept上的System.AggregateException(System.AggregateException on Socket.EndAccept with TaskFactory.FromAsync)

我正在使用套接字(.Net 4 - VS 2010 SP1)进行异步操作,似乎一切正常。 但是,在编写并运行自动测试后,它会传递绿色但显示异常消息:

---- UNHANDLED EXCEPTION ---- Thread Name: <No Name> System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. ---> System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net.Sockets.Socket'. at System.Net.Sockets.Socket.EndAccept(IAsyncResult asyncResult) at P2PNet.Listener.<ListenForConnections>b__0(IAsyncResult r) in C:\Users\lucas.ontivero\Documents\Visual Studio 2010\Projects\P2PNet\P2PNet\Listener.cs:line 76 at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endMethod, TaskCompletionSource`1 tcs) --- End of inner exception stack trace --- at System.Threading.Tasks.TaskExceptionHolder.Finalize() ---> (Inner Exception #0) System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net.Sockets.Socket'. at System.Net.Sockets.Socket.EndAccept(IAsyncResult asyncResult) at P2PNet.Listener.<ListenForConnections>b__0(IAsyncResult r) in C:\Users\lucas.ontivero\Documents\Visual Studio 2010\Projects\P2PNet\P2PNet\Listener.cs:line 76 at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endMethod, TaskCompletionSource`1 tcs)<---

我知道该异常意味着什么,这意味着在调用EndAccept方法之前套接字已关闭。 我没有遇到任何问题,我真正不知道的是如何以优雅的方式防止这种异常。 这是我的代码:

private void ListenForConnections() { try { Task.Factory.FromAsync<Socket>(_listener.BeginAccept, _listener.EndAccept, _listener) .ContinueWith(task => { if (task.IsFaulted) return; ListenForConnections(); var newSocket = task.Result; RaiseClientConnectedEvent(new ConnectionEventArgs(newSocket)); }, TaskContinuationOptions.OnlyOnRanToCompletion); } catch (ObjectDisposedException) { }

我试过这条线:

if (task.IsFaulted) return;

还有:

.ContinueWith(task=>{}, TaskContinuation.OnlyOnFault);

但无论如何都会抛出异常。 哪种方法可以防止异常?

谢谢!

I am working with async operations with sockets (.Net 4 - VS 2010 SP1) and all seems to be working okay. However, after write and run an automated test, it pass green but displays an exception message:

---- UNHANDLED EXCEPTION ---- Thread Name: <No Name> System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. ---> System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net.Sockets.Socket'. at System.Net.Sockets.Socket.EndAccept(IAsyncResult asyncResult) at P2PNet.Listener.<ListenForConnections>b__0(IAsyncResult r) in C:\Users\lucas.ontivero\Documents\Visual Studio 2010\Projects\P2PNet\P2PNet\Listener.cs:line 76 at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endMethod, TaskCompletionSource`1 tcs) --- End of inner exception stack trace --- at System.Threading.Tasks.TaskExceptionHolder.Finalize() ---> (Inner Exception #0) System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net.Sockets.Socket'. at System.Net.Sockets.Socket.EndAccept(IAsyncResult asyncResult) at P2PNet.Listener.<ListenForConnections>b__0(IAsyncResult r) in C:\Users\lucas.ontivero\Documents\Visual Studio 2010\Projects\P2PNet\P2PNet\Listener.cs:line 76 at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endMethod, TaskCompletionSource`1 tcs)<---

I know what that exception means, it means the socket was closed before the EndAccept method was called. I have no problems with that, what I really don´t know is how to prevent that exception in an elegant way. This is my code:

private void ListenForConnections() { try { Task.Factory.FromAsync<Socket>(_listener.BeginAccept, _listener.EndAccept, _listener) .ContinueWith(task => { if (task.IsFaulted) return; ListenForConnections(); var newSocket = task.Result; RaiseClientConnectedEvent(new ConnectionEventArgs(newSocket)); }, TaskContinuationOptions.OnlyOnRanToCompletion); } catch (ObjectDisposedException) { }

I´ve tried with the line:

if (task.IsFaulted) return;

and also with:

.ContinueWith(task=>{}, TaskContinuation.OnlyOnFault);

But the exception is thrown anyway. Which is the way to prevent the exception?

Thank you!

最满意答案

你的路线:

if (task.IsFaulted) return;

返回没有故障,因为您正在检查继续任务的状态,而不是前面的任务。 把它改成这个:

private void ListenForConnections() { Task<Socket> listentask = Task.Factory.FromAsync<Socket>(_listener.BeginAccept, _listener.EndAccept, _listener); listentask.ContinueWith(task => { if (listentask.IsFaulted) { //observe exception Exception exception = listentask.Exception; return; } ListenForConnections(); var newSocket = listentask.Result; RaiseClientConnectedEvent(new ConnectionEventArgs(newSocket)); }); //don't forget to start it listentask.Start(); }

Your line:

if (task.IsFaulted) return;

Is returning not faulted because you are checking the continuation task's status, not the preceding task. Change it to this:

private void ListenForConnections() { Task<Socket> listentask = Task.Factory.FromAsync<Socket>(_listener.BeginAccept, _listener.EndAccept, _listener); listentask.ContinueWith(task => { if (listentask.IsFaulted) { //observe exception Exception exception = listentask.Exception; return; } ListenForConnections(); var newSocket = listentask.Result; RaiseClientConnectedEvent(new ConnectionEventArgs(newSocket)); }); //don't forget to start it listentask.Start(); }

更多推荐

本文发布于:2023-08-06 19:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455322.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Socket   FromAsync   TaskFactory   AggregateException   System

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!