HI, I have the following code that runs in a thread, what the code does is
call a url, and read data from it. The main app need a way to terminate the
read anytime, and call the url again, which I use _reconnect to handle. The
loop will keep on reading data until the value of _reconnect set to true.
Then it will create a new connection and read again. There are two issues:
1, The readline doesn't return until it read a line, so before that, I can
not check _reconnect.
2, It seems I the release to request doesn't work well, even I terminate the
request and create a new one with WebRequest.Create, quite ofter it gaves
error says disposednetworkstream can not be used.
Any idea? please help!
Thanks!
while(!_disposed)
{
try
{
_reconnect = false;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader (response.GetResponseStream());
string line="", message="";
while(!_reconnect && !_disposed && (line=reader.ReadLine())!=null)
{
if(line.StartsWith("///LLIFT//"))
{
if(_onRelayMessage!=null && message!="")
{
_onRelayMessage(this,message);
message="";
}
}
else if(line!="")
message += line;
}
if(request!=null)
try
{
request.Abort();
}
catch
{
}
if(reader!=null)
reader.Close();
if(response!=null)
response.Close();
}
catch(Exception e)
{
string a=e.Message;
}
}