c# - how to implement stream readtimeout property -
i'm fixing bug : app hang in download process 5 minutes while losing network connection. need reduce hanging time 20 seconds.
log snippet:
severe 2016-01-27 , 11:03:14 在 httpsdownload.dodownloadbyhttps(filedownloadinfo fileinfo, downloadingdelegate downdelegate) message: init method:get info 2016-01-27 , 11:08:29 在 httpsdownload.writetofile(filedownloadinfo fileinfo, stream stream, downloadingdelegate downdelegate, boolean clear) message: exception while reading response stream exception: message: time out type: system.net.webexception 在 system.net.connectstream.read(byte[] buffer, int32 offset, int32 size) 在 httpsdownload.writetofile(filedownloadinfo fileinfo, stream stream, downloadingdelegate downdelegate, boolean clear)
code snippet:
private void dodownloadbyhttps(filedownloadinfo fileinfo, downloadingdelegate downdelegate) { //code webresponse = (httpwebresponse)webrequest.getresponse(); responsestream = webresponse.getresponsestream(); writetofile(fileinfo, responsestream, downdelegate, bclear); } private void writetofile(filedownloadinfo fileinfo, stream stream, downloadingdelegate downdelegate, bool clear) { int count = stream.read(readbuffer, 0, blocksize); while (count > 0) { //code count = stream.read(readbuffer, 0, blocksize); } }
i believe timeout occurs in stream.read
according log. easiest way trick should stream.readtimeout = 20000
, can't set readtimeout
property. way can think of write class override readtimeout like:
public class mystream : stream { //code public override int readtimeout { get; set; } //code }
but it's not preferred. better way fix bug?
the timeout needs set in webrequest
object, of type httpwebrequest
.
check out: https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.readwritetimeout(v=vs.110).aspx
Comments
Post a Comment