I am using System.threading.timer in a windows service. But the timer did not complete successfully. Below is the code.
protected override void OnStart(string[] args) { try { eventLog1.WriteEntry("In OnStart"); TimeSpan dueMinutes = TimeSpan.FromMinutes(1); TimeSpan fromMinutes = TimeSpan.FromMinutes(1); System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(CallBack), null, dueMinutes, fromMinutes); } catch (Exception ex) { if (!System.Diagnostics.EventLog.SourceExists("MySource")) { System.Diagnostics.EventLog.CreateEventSource("MySource", "MyEventLog"); } eventLog1.Source = "MySource"; eventLog1.Log = "MyEventLog"; eventLog1.WriteEntry("Error : " + ex.Message); } } public static void CallBack(object sender) { try { DBSyncHandler sync = new DBSyncHandler(); sync.startSync(); } catch (Exception ex) { EventLog eventLog1 = new EventLog(); if (!System.Diagnostics.EventLog.SourceExists("MySource")) { System.Diagnostics.EventLog.CreateEventSource("MySource", "MyEventLog"); } eventLog1.Source = "MySource"; eventLog1.Log = "MyEventLog"; eventLog1.WriteEntry("Error : " + ex.Message); } }
After a successful installation. My workstation is restarting. When the machine restarts, the service is successful. But as soon as the service is called for the first time, it is not repeated for the next time, i.e. the service is not called again.
source share