c# - How to abort an async wait for a pipe connection? -


in following code

namespace ns {     public partial class form1 : form     {         public form1()         {             initializecomponent();         }          string processcommand(string cmd)         {             return "i got " + cmd;         }          streamreader d_sr;         streamwriter d_sw;         namedpipeserverstream d_pipe;         iasyncresult d_ar;          void connected(iasyncresult ar)         {             d_pipe.endwaitforconnection(ar);             d_sw.autoflush = true;             while (true)             {                 var cmd = d_sr.readline();                 if (cmd == null)                     break;                 var answer = processcommand(cmd);                 d_sw.writeline(answer);             }             d_pipe.disconnect();             d_ar = d_pipe.beginwaitforconnection(connected, null);         }          private void form1_load(object sender, eventargs e)         {             d_pipe = new namedpipeserverstream("vatm", pipedirection.inout, 1, pipetransmissionmode.message, pipeoptions.asynchronous);             d_sr = new streamreader(d_pipe);             d_sw = new streamwriter(d_pipe);             d_ar = d_pipe.beginwaitforconnection(connected, null);         }          private void form1_formclosed(object sender, formclosedeventargs e)         {             d_pipe.endwaitforconnection(d_ar);             d_pipe.dispose();         }     } } 

when go close form, waits in endwaitforconnection. i'm going tell pipe not wait client anymore , abort. tried d_pipe.close() instead of calling endwaitforconnection, exception @ endwaitforconnection called in connected.

an unhandled exception of type 'system.objectdisposedexception' occurred in system.core.dll additional information: cannot access closed pipe.

any idea?

i changed this:

namespace ns {     public partial class form1 : form     {         public form1()         {             initializecomponent();         }          string processcommand(string cmd)         {             return "i got " + cmd;         }          streamreader d_sr;         streamwriter d_sw;         namedpipeserverstream d_pipe;         iasyncresult d_ar;          void connected(iasyncresult ar)         {             try             {                 d_pipe.endwaitforconnection(ar);                 d_sw.autoflush = true;                 while (true)                 {                     var cmd = d_sr.readline();                     if (cmd == null)                         break;                     var answer = processcommand(cmd);                     d_sw.writeline(answer);                 }                 d_pipe.disconnect();                 d_ar = d_pipe.beginwaitforconnection(connected, null);             }             catch (system.objectdisposedexception)             {                 // nothing             }         }          private void form1_load(object sender, eventargs e)         {             d_pipe = new namedpipeserverstream("vatm", pipedirection.inout, 1, pipetransmissionmode.message, pipeoptions.asynchronous);             d_sr = new streamreader(d_pipe);             d_sw = new streamwriter(d_pipe);             d_ar = d_pipe.beginwaitforconnection(connected, null);         }          private void form1_formclosed(object sender, formclosedeventargs e)         {             d_pipe.close();         }     } } 

switch asynchronous version: beginwaitforconnection.

if ever complete, you'll need flag completion handler can call endwaitforconnection absorbing exceptions , exiting (call end... ensure resources able cleaned up). enter link description here

check this


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -