I'm building a windows service that grabs information from a website periodically How can I start looking again when a exception is caught? for example when internet goes down and up later.
Code:
public string cadena(string pagina)
{
try
{
String cadena;
WebRequest myWebRequest = WebRequest.Create(pagina);
myWebrequest = 10000;
WebResponse myWebResponse = myWebRequest.GetResponse();
Stream ReceiveStream = myWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("ISO-8859-1");
StreamReader readStream = new StreamReader(ReceiveStream, encode);
cadena = readStream.ReadToEnd();
readStream.Close();
myWebResponse.Close();
return cadena;
}
catch (WebException error)
{
myTimer.Enabled = true;
return "error";
}
}
public void inicia(object sender, System.Timers.ElapsedEventArgs e)
{
myTimer.Enabled = false;
String strSite = cadena("www.something.com");
//Do something with strSite...
myTimer.Enabled = true;
}
protected override void OnStart(string[] args)
{
myTimer = new System.Timers.Timer();
myTimer.Interval = 1500;
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(inicia);
myTimer.Enabled = true;
}