views:

219

answers:

1

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;            
            }
+1  A: 

Here is a very crude example to get you started, this will keep your program from continuing until you get a valid response. There are better ways of doing this, but this is the idea you want. Good luck!

myWebResponse = myWebRequest.GetResponse();
while (myWebResponse.ContentLength == -1) {  
  //sleep
 myWebResponse = myWebRequest.GetResponse();
}  

//continue execution

You should probably put in a check to see if the remote site is available, and either poll until it is, or interrupt when the service is available. This way you won't have to recover from a thrown exception.

I think using exceptions should be reserved for things a little more catastrophic to your application.

Robert Greiner
Thanks for the reply. I already tried this, but in the middle of the poll if the internet goes out, still hangs there and throw an exception.
Hrm, I bet that has something to do with the command you try when polling, can you post that code so we can take a look at it?
Robert Greiner
Well the function cadena only returns the string with the data, and I get the information I need from the string, so the polling consist in assign the value to the string.
by the time you check to see if cadena() returns a string, it is too late and your exception has already been thrown. You need to poll for a valid connection before you start trying to read from your remote site.
Robert Greiner
that make sense, what could be the best way to valid the connection in this case?
I posted an example to get you started, also, can you post the exception you are getting?
Robert Greiner
Thnx for the code, the solution worked but now the problem is that some sites return -1 for ContentLength.