2014-10-03 84 views
-1

我的Windows服务突然不工作,并开始促使错误1053错误1053:该服务没有启动或控制请求及时

这是我下面的程序在C#

using System; 
using System.Net; 
using System.IO; 
using System.Data; 
using System.ComponentModel; 
using System.Collections.Generic; 
using System.Collections; 
using System.Text; 
using System.ServiceProcess; 
using System.Runtime.InteropServices; 
using System.Diagnostics; 
using EPocalipse.IFilter; 
using System.Timers; 
using System.Threading; 
using System.Text.RegularExpressions; 
using System.Windows.Forms; 
using System.Net.Mail; 
using System.Xml; 

namespace MyWindowsService 
{ 
    class Program : ServiceBase 
    { 

     System.Timers.Timer timer = new System.Timers.Timer(); 

     public static void Main(string[] args) 
     { 

      // If we are in debug mode, start it as a console app 
      #if DEBUG 
       Program instance = new Program(); 
       instance.OnStart(null); 
       //Console.ReadLine(); 
       instance.OnStop(); 
      // If we are NOT in debug mode, start it as a service 
      #else 
       ServiceBase.Run(new Program()); 
      #endif 
     } 

     public Program() 
     { 
      CanPauseAndContinue = false; 
      this.ServiceName = "MySciente Service 2012"; 
     } 

     protected override void OnStart(string[] args) 
     { 

      base.OnStart(args); 
      //System.Threading.Thread.Sleep(20 * 60 * 1000); 
      startMyService(); 


     } 

     protected override void OnStop() 
     { 
      base.OnStop(); 
      timer.Enabled = false; 
      //sendMail(); 

     } 

     private void InitializeComponent() 
     { 

     } 

     private void startMyService() 
     { 
      fileMigration(); 

      timer.Elapsed += new ElapsedEventHandler(OnElapsedTime); 
      timer.Interval = (15 * 60 * 1000); //15 is minutes, so here is 15 minutes 
      timer.Enabled = true; 
     } 
     private void OnElapsedTime(object source, ElapsedEventArgs e) 
     { 
      fileMigration(); 
     } 

     public static void fileMigration() 
     { 

      --My Program-- 

     } 


    } 
} 

我可以知道它是什么原因造成的?之后windows &反病毒更新我的服务从上个月引起这个问题15th

+0

发生此错误时, onstart方法需要很长的时间来终止,我建议你看看事件查看器的更多信息 – mfarouk 2014-10-03 08:48:04

回答

0

显然fileMigration()花费的时间太长。在服务启动期间,您不应该执行繁重的操作。我不知道你提到的那些更新和这个问题之间的关系,这可能是巧合,或者反病毒现在需要更多时间在他们的文件系统钩子上,导致fileMigration()需要更长的时间。

如果你想fileMigration()在启动时运行,请考虑使用TaskThreadPool.QueueUserWorkItem到一个单独的线程运行,同时通报有关操作系统成功启动初期,即:

private void startMyService() 
    { 
     Task.Factory.StartNew(fileMigration); 

     timer.Elapsed += new ElapsedEventHandler(OnElapsedTime); 
     timer.Interval = (15 * 60 * 1000); //15 is minutes, so here is 15 minutes 
     timer.Enabled = true; 
    } 
+0

如何在我的程序中使用Task或ThreadPool.QueueUserWorkItem,给出一些示例 – Aravinthan 2014-10-03 09:10:54

+0

@Aravinthan添加了一个示例。 – 2014-10-03 09:56:33

+0

@Evenhuis这个解决方案也不适合我 – Aravinthan 2014-10-03 10:12:42

相关问题