System.timers.timer自行停止 [英] System.timers.timer stopping on its own

查看:457
本文介绍了System.timers.timer自行停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows服务器环境中csharp中的计时器似乎存在问题。我的应用程序中有一个计时器,每分钟都会触发,但是在大约2天后就会停止,我对于为什么会发生这种情况感到茫然。 />


我已经在mvc应用程序上尝试了这些计时器,因为只需简单的url调用内部API,它在Windows 7和服务器环境中运行良好,将近2天后停止。我的源代码:



更新:似乎计时器在应用程序池回收周围停止。是否有我需要的设置配置以便app计时器重新开始?



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?

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;
           }

       }





我的尝试:



我也尝试了线程计时器,这更糟糕,因为它在大约10分钟后完全停止。



What I have tried:

I have tried Threading timers as well and that is even worse as it stops completely after about 10 mins.

推荐答案

如果您使用的是默认设置,您的应用程序将在1740分钟(29小时)后销毁,届时托管它的池将会回收...

来自您的代码/说明你似乎不理解Web应用程序的目标和目的,并尝试使用它来代替服务......如果没有巨大的努力,这将无法工作,即便如此,它所做的只是耗尽所有资源并打破机器......尝试为问题选择合适的技术......
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...


这篇关于System.timers.timer自行停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆