C# TaskFactory.FromAsync的Socket.EndAccept上的System.AggregateException
我正在使用sockets(.net4-vs2010sp1)进行异步操作,一切似乎都正常工作。但是,在写入并运行自动测试后,它通过绿色测试,但显示异常消息:C# TaskFactory.FromAsync的Socket.EndAccept上的System.AggregateException,c#,sockets,asynchronous,.net-4.0,C#,Sockets,Asynchronous,.net 4.0,我正在使用sockets(.net4-vs2010sp1)进行异步操作,一切似乎都正常工作。但是,在写入并运行自动测试后,它通过绿色测试,但显示异常消息: ---- UNHANDLED EXCEPTION ---- Thread Name: <No Name> System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing it
---- 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)<---
以及:
.ContinueWith(task=>{}, TaskContinuation.OnlyOnFault);
但无论如何都会抛出异常。防止异常的方法是什么
谢谢大家! 您的线路:
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();
}
private void ListenForConnections(){
Task listentask=Task.Factory.fromsync(_listener.beginacept、_listener.EndAccept、_listener);
listentask.ContinueWith(任务=>{
if(listentask.IsFaulted){
//观察异常
Exception=listentask.Exception;
返回;
}
ListenForConnections();
var newSocket=listentask.Result;
RaiseClientConnectedEvent(新的ConnectionEventArgs(newSocket));
});
//别忘了启动它
listentask.Start();
}
您需要通过读取异常
属性来观察异常。在你的续集里这样做
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();
}