hi,
i have a windows service which is checking to the desire URL and check back its httpwebresponse data. and, i save the result in a log file insdie local drive. i use a timer to trigger my service's function that check to my desire URL response. the problem is with that timer. since my timer's data type is double, my friend suggest that if it reach to its maximum value my service will get error or it will stop. he had faced with that situation before, but couldn't find out a solution. so, i'ld like to know if it is true, is there any solution to triger my service's function at every 5 minute or at a require interval, like i used to check with timer.
and, here is some of the code that i used with.
thanks,
myo zaw
public main() { double interval = 300000; myTimer = new Timer(interval); myTimer.Elapsed += new ElapsedEventHandler(svsTimer_Elapsed); } void svsTimer_Elapsed(object sender, ElapsedEventArgs e) { WebRequest wReq = HttpWebRequest.Create("http://www.yahoo.com"); HttpWebResponse wRes = (HttpWebResponse)wReq.GetResponse(); string strResult = Res.StatusCode.GetHashCode().ToString(); } |

timer problem
AnanthK
Thanks Mill,
I now get the point.
Axel.C
Not 100% certain I understand what you are trying to explain, but what I can recommend in general a good practice is anytime you are dealing with a portion of code that can throw an exception, you should wrap it around a Try-Catch block at the very least. That way you can deal with any exceptions that may occur and decide if you want to hault operation or if its something you can recover from then resume normal program flow.
So, in your code there, here's one thing you may do if you are worried about an exception occuring when retrieving the web response.
void svsTimer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
WebRequest wReq = HttpWebRequest.Create("http://www.yahoo.com");
HttpWebResponse wRes = (HttpWebResponse)wReq.GetResponse();
string strResult = Res.StatusCode.GetHashCode().ToString();
}
Catch(Exception ex)
{
// Handle exception here.
}
}
kamran khawaja
I don't see how you could get an error strictly on your interval unless you are changing the interval at run time. If you are not changing the interval, what you have there is fine. The interval is a value that the class uses to countdown before firing the Elapsed event. Timer is designed strictly for this purpose in a scenario where you need to constantly do some specific processing at the specified interval. There is no limit (theoretical) to how many times the event can be fired. The interval is not a value thats normally messed with after you set it in most circumstances. The timer class does not change the interval on its own unless you do.
When the Elapsed even is fired it is raised on a threadpool thread. Now, I haven't checked in .NET 2.0 how unhandled thread exceptions are handled, but if I recall, when an unhandled exception happens in .NET 1.1 on a threadpool thread, the CLR just terminates the thread and doesn't stop program execution unlike the main thread. So, that's something you may want to know in advance as well when working with the Timer class.
manicm
hi mills,
i already did use that try-catch block at my web request and response part, like what you'd recommended me above here, inside my real program. what i'm worrying about is for my interval. since i'm using timer to check up the desire URL's web response at 24/7 situation, can that interval give me error if it reach to its maximum double data type range
myo zaw
Tiendq
The Timer component will not work in a windows Service,u should use the System.Threading.Timer, read this:
http://msdn2.microsoft.com/en-us/library/2x96zfy7.aspx