Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There seems to be a problem with Timers in csharp in a windows server environment.I have a timer in my application that fires every minute but it stops after about 2 days and i am dumbfounded as to why this is happening.

I have tried these timers on an mvc application as just simple url calls to an internal API which works really well on windows 7 and on a server environment stops after almost 2 days. My source code:

Update: It seems as though the timer stops around the app pool recycle.Is there a setting that i need to configure in order for the app timer to start again?

C#
int smsCounter = 0;
       int smsCounterQuery = 0;
       protected void Application_Start()
       {
           // Dynamically create new timer
           Timer smsTimer = new Timer
           {
               Interval = 1000,
               Enabled = true
           };

           // Add handler for Elapsed event
           smsTimer.Elapsed += CallUrls;
           smsTimer.Stop();
           smsTimer.Start();

           AreaRegistration.RegisterAllAreas();
           GlobalConfiguration.Configure(WebApiConfig.Register);
           FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
           RouteConfig.RegisterRoutes(RouteTable.Routes);
           BundleConfig.RegisterBundles(BundleTable.Bundles);
       }

       public void CallUrls(object sender, ElapsedEventArgs e)
       {
           smsCounter++;
           smsCounterQuery++;

           if (smsCounter == 60)
           {
               //local pc url:
               //string url = "http://localhost:49788/Sms/sendMultiSMS";

               //server url:
               string url = "http://localhost:105/Sms/sendMultiSMS";
               var request = (HttpWebRequest)HttpWebRequest.Create(url);
               request.Method = "GET";


               using (StreamWriter writer = new StreamWriter("C:\\In\\smsheartbeat.txt", true))
               {
                   writer.WriteLine("Run SMS Sending method at time: " + DateTime.Now.TimeOfDay);

               }


               smsCounter = 0;
           }

           if (smsCounterQuery == 600)
           {
               //testing:
               //string url = "http://localhost:49788/Sms/sendLogByDate";

               //server url:
               string url = "http://localhost:105/Sms/sendLogByDate";
               var request = (HttpWebRequest)HttpWebRequest.Create(url);
               request.Method = "GET";
               var response = (HttpWebResponse)request.GetResponse();


               using (StreamWriter writer = new StreamWriter("C:\\In\\smsheartbeat.txt", true))
               {
                   writer.WriteLine("Date range search url method fired at time: " + DateTime.Now.TimeOfDay);

               }

               smsCounterQuery = 0;
           }

       }


What I have tried:

I have tried Threading timers as well and that is even worse as it stops completely after about 10 mins.
Posted
Updated 23-Feb-16 20:14pm

1 solution

If you are using default settings, your application will be destroyed after 1740 minutes (29 hours), when the pool hosting it will recycle...
From you code/explanation it seems that you do not understand the goal and purpose of a web application and try to use it a service instead...That will not work without enormous efforts, and even then all it will do is to eating up all your resources and break the machine...Try to choose the right technology for the problem...
 
Share this answer
 
Comments
JDevZA 24-Feb-16 2:19am    
Yes you are right here.i do have a service that is doing the same job but thought it can also do the same task here in the web application and not have 2 points of conflict/interest should something happen to the service or api.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900