C#windows服务CPU利用率很高

编程入门 行业动态 更新时间:2024-10-25 16:29:54
本文介绍了C#windows服务CPU利用率很高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在运行此服务时创建了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利用率很高

本文发布于:2023-11-03 20:05:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555964.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:率很高   windows   CPU

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!