c# - System.AggregateException on Socket.EndAccept with TaskFactory.FromAsync -


i working async operations sockets (.net 4 - vs 2010 sp1) , seems working okay. however, after write , run automated test, pass green displays exception message:

---- unhandled exception ---- thread name: <no name> system.aggregateexception: task's exception(s) not observed either waiting on  task or accessing exception property. result, unobserved exception    rethrown finalizer thread. ---> system.objectdisposedexception: cannot access    disposed object. object name: 'system.net.sockets.socket'.     @ system.net.sockets.socket.endaccept(iasyncresult asyncresult)     @ p2pnet.listener.<listenforconnections>b__0(iasyncresult r) in  c:\users\lucas.ontivero\documents\visual studio  2010\projects\p2pnet\p2pnet\listener.cs:line 76     @ system.threading.tasks.taskfactory`1.fromasynccorelogic(iasyncresult iar, func`2  endmethod, taskcompletionsource`1 tcs)     --- end of inner exception stack trace ---     @ system.threading.tasks.taskexceptionholder.finalize() ---> (inner exception #0) system.objectdisposedexception: cannot access disposed object. object name: 'system.net.sockets.socket'.     @ system.net.sockets.socket.endaccept(iasyncresult asyncresult)     @ p2pnet.listener.<listenforconnections>b__0(iasyncresult r) in   c:\users\lucas.ontivero\documents\visual studio    2010\projects\p2pnet\p2pnet\listener.cs:line 76     @ system.threading.tasks.taskfactory`1.fromasynccorelogic(iasyncresult iar, func`2 endmethod, taskcompletionsource`1 tcs)<---   

i know exception means, means socket closed before endaccept method called. have no problems that, don´t know how prevent exception in elegant way. 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 line:

if (task.isfaulted) return;  

and with:

.continuewith(task=>{}, taskcontinuation.onlyonfault); 

but exception thrown anyway. way prevent exception?

thank you!

your line:

if (task.isfaulted) return;  

is returning not faulted because checking continuation task's status, not preceding task. change 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 start         listentask.start();     } 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -