/// <summary>Timer task executed, for each url, every pollingInterval or _retryOnErrorInterval.</summary> /// <param name="StateObj">The state object.</param> private void TimerTask(object StateObj) { try { if (StateObj != null) { StateObjClass stateObj = StateObj as StateObjClass; if (stateObj != null) { try { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(stateObj.url); request.Credentials = System.Net.CredentialCache.DefaultCredentials; request.Method = System.Net.WebRequestMethods.Http.Head; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { if (response.StatusCode != HttpStatusCode.OK) { throw new WebException("Server response not valid : " + response.StatusCode); } } } catch (Exception ex) { if (!stateObj.errorMode) { eventLog.WriteEntry("Timeout with url " + stateObj.url + ". Error : " + ex.Message, EventLogEntryType.Error); stateObj.timerReference.Change(0, _retryOnErrorInterval); stateObj.errorMode = true; } } if (stateObj.timerCanceled) // Dispose Requested. { stateObj.timerReference.Dispose(); eventLog.WriteEntry("Done with url " + stateObj.url, EventLogEntryType.Information); } } } } catch (System.Exception exception) { eventLog.WriteEntry("Unexpected error in PISGroundKicker.TimerTask : " + exception.ToString(), EventLogEntryType.Error); } }
/// <summary> /// When implemented in a derived class, executes when a Start command is sent to the service by /// the Service Control Manager (SCM) or when the operating system starts (for a service that /// starts automatically). Specifies actions to take when the service starts. /// </summary> /// <param name="args">Data passed by the start command.</param> protected override void OnStart(string[] args) { string urls = System.Configuration.ConfigurationManager.AppSettings["WebServicesUrls"]; if (!String.IsNullOrEmpty(urls)) { foreach (string url in urls.Split(UrlsSeparator, StringSplitOptions.RemoveEmptyEntries)) { StateObjClass stateObj = new StateObjClass(); stateObj.url = url; stateObj.timerCanceled = false; stateObj.errorMode = false; System.Threading.TimerCallback TimerDelegate = new System.Threading.TimerCallback(TimerTask); System.Threading.Timer TimerItem = new System.Threading.Timer(TimerDelegate, stateObj, 0, _pollingInterval); stateObj.timerReference = TimerItem; _stateObjList.Add(stateObj); eventLog.WriteEntry("Starting thread for url " + stateObj.url, EventLogEntryType.Information); } } }