c# - How do I create a task to read data continuously from a TcpClient using tasks and async methods -
i trying read data continuously tcpclient. here code snippet.
var task = task.factory.startnew(read); task.continuewith(ant => console.writeline("error: " + ant.exception.message), taskcontinuationoptions.onlyonfaulted); task.continuewith(ant => { log("log data"); }); void read() { task<int> readchunk = task<int>.factory.fromasync( _stream.beginread, _stream.endread, data, bytesread, data.length - bytesread, null, taskcreationoptions.attachedtoparent); readchunk.continuewith(rt => { bytesread = readchunk.result; }, taskcontinuationoptions.notonfaulted | taskcontinuationoptions.attachedtoparent); }
this bit of code works fine, there maybe more data coming on stream, want go , read again. starting read task doesn't seem right. not familiar tasks parallel library. have written this:
while (true) { bytesread += _stream.read(data, bytesread, chunksize); if(bytesread == bytesexpected) break; }
Comments
Post a Comment