我期待着解決我的Windows服務的特定問題。它使用了大約25%的CPU,我認爲,對於它必須完成的任務來說,這是非常誠實的方式。如何減少Windows服務器的高CPU使用率?
windows服務包括3個線程,用作filewatcher。如果創建了一個特定類型的文本文件,他們會啓動一個存儲過程在一個SQL服務器上,具體取決於:..
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 us如果其他進程需要CPU使用率,則年齡會降低到最低程度..目前有幫助,但我認爲這不是最終的解決方案。
因此,大概你們中的一個人可以教我,我可以改進什麼,可能爲什麼我必須改變它,以獲得更好的表現?!
那太棒了!先謝謝你。 :-)
PS:我知道我的方式初始化三個觀察是不是最好的方式,但是這不應該是我認爲這一點..
PPS:我實現了查詢的執行異步,因爲這是非常長的運行隊列,沒有這個實現,我遇到了超時問題。^^
'while(started){}'是問題所在。把它拿出來。 – stuartd
可能是這樣的:while(started) { } – Botonomous
將代碼放入後臺工作者可能會更好,因此它不會停止發生PC上的其他處理。在控制檯應用程序上刪除while將刪除該塊,程序將終止。您可以使用WaitHandler來阻止哪一個會消除while循環不斷運行。 – jdweng