I've got an open connection with a stream reader pulling data in to my system and storing in a database. Everything works great, except occasionally the stream from the remote server stops and this makes my program crash.
How can I catch this situation when it occurs, and maybe attempt to reconnect. The streamreader code is:
using (stream)
{
using (StreamReader sr = new StreamReader(stream))
{
while (!sr.EndOfStream && running)
{
string s = sr.ReadLine();
var del = this.OnLineReceived;
if (del != null)
{
del(this, new LineReceivedArgs(s));
}
}
}
}
The code fails on the 'while' loop and I'm not sure how I can catch it...