如何减少windows服务的高CPU使用率
本文关键字:CPU 使用率 服务 何减少 windows | 更新日期: 2023-09-27 18:15:53
我期待解决我的windows服务的一个具体问题。它使用了大约25%的CPU,我认为,对于它必须完成的任务来说,这是一种诚实的方式。
windows服务包括3个线程,它们被用作文件监视器。如果创建了特定类型的文本文件,则会在sql server上启动存储过程,具体取决于。
3个filewatchers,因为有3个文件类型,我必须注意。
这是我当前使用的代码:
watcher.cs:
internal class Watcher
{
private List<FileSystemWatcher> _fileWatcher;
private Regex _regex;
public bool started;
public Watcher()
{
started = false;
_fileWatcher = new List<FileSystemWatcher>();
_regex = new Regex(@"(?<=Anz_)(.*)(.txt*@)(.*)");
initAllWatcher();
}
private void initAllWatcher()
{
// LBH:
var watcher = new FileSystemWatcher();
watcher.Filter = ConfigurationManager.AppSettings["FilterLBH"];
watcher.Path = ConfigurationManager.AppSettings["FilePath"];
watcher.Created += onCreated;
_fileWatcher.Add(watcher);
// LSC:
watcher = new FileSystemWatcher();
watcher.Filter = ConfigurationManager.AppSettings["FilterLSC"];
watcher.Path = ConfigurationManager.AppSettings["FilePath"];
watcher.Created += onCreated;
_fileWatcher.Add(watcher);
// LEM:
watcher = new FileSystemWatcher();
watcher.Filter = ConfigurationManager.AppSettings["FilterLEM"];
watcher.Path = ConfigurationManager.AppSettings["FilePath"];
watcher.Created += onCreated;
_fileWatcher.Add(watcher);
}
public void Run()
{
foreach (var filewatcher in _fileWatcher)
{
filewatcher.EnableRaisingEvents = true;
}
started = true;
Thread.CurrentThread.IsBackground = true;
Thread.CurrentThread.Priority = ThreadPriority.Lowest;
while (started)
{
}
}
private async void onCreated(object Source, FileSystemEventArgs Arg)
{
await execute(Arg);
}
private async Task execute(FileSystemEventArgs Arg)
{
string tablename = _regex.Split(Arg.Name)[1];
string targetDatabase = _regex.Split(Arg.Name)[3];
if (tablename.Equals("") == false)
{
if (targetDatabase.Equals("") == false)
{
using (var dbConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString))
{
var command = new SqlCommand(ConfigurationManager.AppSettings["StoredProcedure"], dbConnection);
command.CommandTimeout = dbConnection.ConnectionTimeout;
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@strParDbTabelle", SqlDbType.VarChar).Value = tablename;
command.Parameters.Add("@strParDbSchema", SqlDbType.VarChar).Value = "dbo";
command.Parameters.Add("@strParDbName", SqlDbType.VarChar).Value = targetDatabase;
try
{
await command.ExecuteNonQueryAsync();
}
catch (System.Exception)
{
throw;
}
}
}
}
}
}
program.cs:
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
private static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new BaanJobActiService()
};
ServiceBase.Run(ServicesToRun);
}
}
因为我在我的代码中添加了这些行:
Thread.CurrentThread.IsBackground = true;
Thread.CurrentThread.Priority = ThreadPriority.Lowest;
如果其他进程需要CPU使用,则CPU使用将降至最低。这是目前有用的,但我认为,这不能是最终的解决方案。
所以也许你们中的一个可以教我,我可以改进什么,为什么我必须改变它,以获得更好的表现?那太棒了!提前谢谢你。: -)
PS:我知道我初始化三个观察者的方式不是最好的方式,但我认为这不应该是重点。
PPS:我实现了查询异步执行,因为这是非常长的运行队列,没有这个实现,我遇到了超时问题。^^
将Thread.Sleep(10)
添加到while循环中为我完成了这项工作。删除循环是不可能的,因为filewatcher没有它就不能工作。
不管怎样. .这个解决方案将CPU使用率降低到0 - 0.5%,与25-30%相比,这是非常好的!