Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to call a method in every 5 seconds?
    text
    copied!<p>I have two wcf services Client.svc and Admin.svc. A method in client calls admin service method in every 5 seconds. And Admin's method which is been called by client verifies that if this method is not called within 5 seconds then update database with status "NotResponding" else update it with "IsAlive" value. </p> <p>All this should be done on a separate thread.</p> <p>I have written some code wherein Client uses Timer to call that method in every 5 seconds.</p> <pre><code> public static void RegisterHeartBeat(PingRequest pingRequest) { try { string heartBeatInterval = Phoenix.API.Utility.ConfigReader.GetAppSettingsValue("HeartBeatInterval"); int timeInSeconds = -1; Int32.TryParse(heartBeatInterval, out timeInSeconds); if (timeInSeconds != -1) { TimerCallback timerCallHeartBeat = new TimerCallback(CallRegisterHeartBeat); Timer timer = new Timer(timerCallHeartBeat, pingRequest, 0, (timeInSeconds * 1000)); //Multiplying by 1000, converts seconds to milliseconds } else { Exception ex = new Exception("HeartBeatInterval is not configured in web.config file"); Phoenix.Client.API.BLL.Common.CommonUtility.CreateResultAndLogClientException(null, null, ex); } } catch (Exception ex) { Phoenix.Client.API.BLL.Common.CommonUtility.CreateResultAndLogClientException(null, null, ex); } } private static void CallRegisterHeartBeat(object state) { PhoenixClientBLL.Admin.InternalClient internalClient = new PhoenixClientBLL.Admin.InternalClient("BasicHttpBinding_IInternal"); if (state != null) { //AdminAPI accepts Admin.PingRequest parameter which has a different format than ClientAPI PingRequest. //Thus, a new object of admin ping request type is created. Phoenix.API.ClientServiceContracts.DataContracts.PingRequest pingRequestDC = state as Phoenix.API.ClientServiceContracts.DataContracts.PingRequest; //AdminAPI PhoenixClientBLL.Admin.PingRequest pingRequest = new PhoenixClientBLL.Admin.PingRequest(); //Test Agent ID pingRequest.TestAgentId = Guid.Parse(pingRequestDC.TestAgentId); //Test Agent Status is not set because it will be decided in ADMIN API as per the interval difference. internalClient.RegisterHeartBeat(pingRequest); } } </code></pre> <p>In Admin, I check the last update date and the current date with the difference of time to update database accordingly. </p> <pre><code> public static void RegisterHeartBeat(PingRequest pingRequest) { int status = 0; DateTime startTime, endTime; int testAgentId = -1; string heartBeatIntervalValue = Phoenix.API.Utility.ConfigReader.GetAppSettingsValue("HeartBeatInterval"); int heartBeatInterval = -1; if(String.IsNullOrEmpty(heartBeatIntervalValue)) { Common.CommonUtility.CreateResultAndLogException(null, null, new Exception("HeartBeatInterval is not configured in the configuration file")); } else { try { string key = pingRequest.TestAgentId.ToString(); if (!String.IsNullOrEmpty(key)) { if (!heartBeatTimeStamp.ContainsKey(key)) { heartBeatTimeStamp.Add(key, System.DateTime.Now); } else { endTime = DateTime.Now; if (heartBeatTimeStamp[key].HasValue) { startTime = heartBeatTimeStamp[key].Value; var timeDiff = new TimeSpan(endTime.Ticks - startTime.Ticks); //Check the configured heart beat interval value Int32.TryParse(heartBeatIntervalValue, out heartBeatInterval); if (heartBeatInterval != -1) { if (timeDiff.Seconds &gt; heartBeatInterval) { // add update NotResponding = 3 .. Int32.TryParse(pingRequest.TestAgentId.ToString(), out testAgentId); //If Test Agent ID is converted into integer than update table else log the error. if (testAgentId != -1) { status = DAO.TestAgentDAO.RegisterHeartBeat(testAgentId, (int)TestAgentStatus.NotResponding); } else { Common.CommonUtility.CreateResultAndLogException(null, null, new Exception("Cannot convert Test Agent ID Data type from GUID to Integer")); } //Sql Error if (0 != status) { Common.CommonUtility.CreateResultAndLogSqlError(null, status, null); } } else { // add update IsAlive= 4 Int32.TryParse(pingRequest.TestAgentId.ToString(), out testAgentId); //If Test Agent ID is converted into integer than update table else log the error. if (testAgentId != -1) { status = DAO.TestAgentDAO.RegisterHeartBeat(testAgentId, (int)TestAgentStatus.IsAlive); } else { Common.CommonUtility.CreateResultAndLogException(null, null, new Exception("Cannot convert Test Agent ID Data type from GUID to Integer")); } //Sql Error if (0 != status) { Common.CommonUtility.CreateResultAndLogSqlError(null, status, null); } } } else { Common.CommonUtility.CreateResultAndLogException(null, null, new Exception("Invalid HeartBeatInterval Value")); } } } } else { Common.CommonUtility.CreateResultAndLogException(null, null, new Exception("Test Agent ID is incorrect or does not exists")); } } catch (Exception ex) { Common.CommonUtility.CreateResultAndLogException(null, null, ex); } } } </code></pre> <p>But my timer behaves in a wierd manner and never calls the admin method.. Can you please check it why? or any other logic needs to be implemented here.</p> <p>Thanks Priyanka</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload