我在运行此服务时创建了windows服务CPU利用率很高.. 请找到下面的代码.. 这里ComputeData()方法调用程序和这个程序以1:23分钟执行时间。已经设置了5分钟的间隔来执行ComputeData()方法。
I have created windows service while running this service CPU utilization high.. Please find the below code.. here ComputeData() method call procedure and this procedure take 1:23 minute to execute time. have set here 5 min interval to execute the ComputeData() method.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.Timers; using System.Configuration; namespace UpdateData { public partial class Service1 : ServiceBase { static Timer timer; public Service1() { InitializeComponent(); } private static void start_timer() { timer.Start(); } protected override void OnStart(string[] args) { timer = new Timer(); timer.Interval = TimeSpan.FromMinutes(5).TotalMilliseconds; timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); timer.Enabled = true; timer.Start(); } public static bool ExecuteTime() { TimeSpan StartingTime = new TimeSpan( Int32.Parse(ConfigurationManager.AppSettings["StartingTimeHour"]), Int32.Parse(ConfigurationManager.AppSettings["StartingTimeMinute"]), 0); TimeSpan EndingTime = new TimeSpan( Int32.Parse(ConfigurationManager.AppSettings["EndingTimeHour"]), Int32.Parse(ConfigurationManager.AppSettings["EndingTimeMinute"]), 0); TimeSpan CurrentTime = DateTime.Now.TimeOfDay; return ((CurrentTime > StartingTime) && (CurrentTime < EndingTime)); } static void timer_Elapsed(object sender, ElapsedEventArgs e) { try { DateTime TodayDate = DateTime.Now; if (TodayDate.DayOfWeek == DayOfWeek.Saturday || TodayDate.DayOfWeek == DayOfWeek.Sunday) { } else { while (ExecuteTime()) { BasicBO.ComputeData(); } } } catch (Exception ex) { string msg = ex.Message + " at " + DateTime.Now + " From timer_Elapsed method"; BasicBO.writeLog(msg, "UpdateVMErrorLog.txt"); } } protected override void OnStop() { string msg = "Service Stoped at : " + DateTime.Now; BasicBO.writeLog(msg, "UpdateVMStopLog.txt"); } } }我尝试了什么: 到目前为止,我已经尝试将时间间隔增加3到5分钟但问题扩大相同。
What I have tried: so far i have tried to increase time interval 3 to 5 min but problem reaming same.
推荐答案将咀嚼CPU的行是 The lines that will be chewing up your CPU are while (ExecuteTime()) { BasicBO.ComputeData(); }
这是一个非常紧凑的循环,没有CPU的喘息空间。在那个循环中弹出一个睡眠,我认为你的问题会消失。 这个,或者你的意思是如果而不是而那里。
你好manish_rcc 请查看修改后的代码和评论: Hi manish_rcc please have a look at the modified Code and the comments: public partial class Service1 : ServiceBase { // 1. Timer not static - one instance per Service... Timer timer; // Configuration can be cached TimeSpan startingTime; TimeSpan endingTime; public Service1() { InitializeComponent(); } private static void start_timer() { timer.Start(); } protected override void OnStart(string[] args) { timer = new Timer(); // hardcoded interval? timer.Interval = TimeSpan.FromMinutes(5).TotalMilliseconds; timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); // no need to call Enable and Start - just Start timer.Enabled = true; timer.Start(); // Read Configuration only once to cache startingTime = new TimeSpan( Int32.Parse(ConfigurationManager.AppSettings["StartingTimeHour"]), Int32.Parse(ConfigurationManager.AppSettings["StartingTimeMinute"]), 0); endingTime = new TimeSpan( Int32.Parse(ConfigurationManager.AppSettings["EndingTimeHour"]), Int32.Parse(ConfigurationManager.AppSettings["EndingTimeMinute"]), 0); } // Better naming: Functions returning booleans should be named with Is/Has/Can... public static bool ShouldExecute() { TimeSpan CurrentTime = DateTime.Now.TimeOfDay; return ((CurrentTime > startingTime) && (CurrentTime < endingTime)); } static void timer_Elapsed(object sender, ElapsedEventArgs e) { try { DateTime TodayDate = DateTime.Now; if (TodayDate.DayOfWeek == DayOfWeek.Saturday || TodayDate.DayOfWeek == DayOfWeek.Sunday) { // Exit early, no need to do the check on weekends (hardcoded values a good idea?) return; } else { // no while here, you are already in a timer(-loop) - just check if operation should run now, timer will come here after 5 minutes (the intervall you specified to the timer, again hardcoded value a good idea?) if(ShouldExecute()) { BasicBO.ComputeData(); } } } catch (Exception ex) { string msg = ex.Message + " at " + DateTime.Now + " From timer_Elapsed method"; BasicBO.writeLog(msg, "UpdateVMErrorLog.txt"); } } protected override void OnStop() { string msg = "Service Stoped at : " + DateTime.Now; BasicBO.writeLog(msg, "UpdateVMStopLog.txt"); } }
你看 - 你好混淆的部分是,一个计时器已代表一个一种循环来检查... 亲切的问候 Johannes
You see - the part you good confused about is, that a timer already represents a Kind of "Loop" to check... Kind regards Johannes
更多推荐
C#windows服务CPU利用率很高
发布评论