2016-11-02 33 views
1

我期待着解决我的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:我实现了查询的执行异步,因为这是非常长的运行队列,没有这个实现,我遇到了超时问题。^^

+6

'while(started){}'是问题所在。把它拿出来。 – stuartd

+5

可能是这样的:while(started) { } – Botonomous

+1

将代码放入后台工作者可能会更好,因此它不会停止发生PC上的其他处理。在控制台应用程序上删除while将删除该块,程序将终止。您可以使用WaitHandler来阻止哪一个会消除while循环不断运行。 – jdweng

回答

0

Thread.Sleep(10)添加到while循环为我完成工作。删除循环是不可能的,因为没有它,filewatcher不能工作。

无论如何..这个解决方案使CPU使用率下降到0 - 0.5%,这比25-30%退出了不错的!

相关问题