2012-08-06 138 views
0

所以我试图运行我编程的服务,由于某种原因,它给我这个错误,当我尝试启动它:开发服务,错误1053

Error 1053: the service did not respond to the start or control request in a timely fashion 

我的代码是非常基本。

static void Main(string[] args) 
    { 
     try 
     { 
      ServiceBase[] ServicesToRun; 
      ServicesToRun = new ServiceBase[] 
      { 
       new Program() 
      }; 
      ServiceBase.Run(ServicesToRun); 
     } 
     catch (Exception ex) 
     { 
      string SourceName = "WindowsService.ExceptionLog"; 
      if (!EventLog.SourceExists(SourceName)) 
      { 
       EventLog.CreateEventSource(SourceName, "Application"); 
      } 

      EventLog eventLog = new EventLog(); 
      eventLog.Source = SourceName; 
      string message = string.Format("Exception: {0} \n\nStack: {1}", ex.Message, ex.StackTrace); 
      eventLog.WriteEntry(message, EventLogEntryType.Error); 
     } 
    } 

    public Program() 
    { 
     this.ServiceName = "FetchFeed"; 
    } 

    protected override void OnStart(string[] args) 
    { 
     base.OnStart(args); 

     //TODO: place your start code here 
    repeat: FetchFeed(); 
     Thread.Sleep(3600000); 
     goto repeat; 
    } 

    protected override void OnStop() 
    { 
     base.OnStop(); 

     //TODO: clean up any variables and stop any threads 
    } 

private static void FetchFeed() 
{ 
    //Some HTTP requests and retrieval. 
} 

,这是安装程序类:

[RunInstaller(true)] 
public class Service_Installer : Installer 
{ 
    public Service_Installer() 
    { 
     ServiceProcessInstaller processInstaller = new ServiceProcessInstaller(); 
     ServiceInstaller serviceInstaller = new ServiceInstaller(); 

     //set the privileges 
     processInstaller.Account = ServiceAccount.LocalSystem; 

     serviceInstaller.DisplayName = "FetchFeed"; 
     serviceInstaller.StartType = ServiceStartMode.Manual; 

     //must be the same as what was set in Program's constructor 
     serviceInstaller.ServiceName = "FetchFeed"; 

     this.Installers.Add(processInstaller); 
     this.Installers.Add(serviceInstaller); 
    } 
} 

可能是什么错误背后的原因是什么?我已经检查过FetchFeed()作为一个独立的应用程序,没有例外。

+0

你必须启动一个线程http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx你在onStart,而不是睡觉 – rene 2012-08-06 13:46:04

+0

事情是我必须每隔1小时运行一次线程,我怎么能这样做,而不会让主进程进入睡眠状态,因为使用Timer会占用更多的资源并且效率不高,如何告诉服务器必须运行每隔一小时穿线? @rene – 2012-08-06 13:52:26

+0

@ gerald-versluis – 2012-08-06 13:53:08

回答

1

使用appopiate timer

public class YourService 
{ 
     var tim = new System.Timers.Timer(60 * 60 * 1000); // 1 hour 

    protected override void OnStart(string[] args) 
    { 
     base.OnStart(args); 

     tim.AutoReset = true; 
     tim.Elapsed += new System.Timers.ElapsedEventHandler(tim_Elapsed); 
     tim.Enabled = true; 
     // first time run 
     ThreadPool.QueueUserWorkItem(WaitCallback(FetchFeed)); 
    } 

    static void tim_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
    { 
      FetchFeed(); 
    } 
} 
+0

所以如何确保FetchFeed()第一次运行而不需要等待1小时?@rene – 2012-08-06 14:33:49

+0

如果FetchFeed在30秒内保证有效,您可以立即调用它。您可以使用第二个计时器,该计时器在autoreset = false秒后运行,或使用ThreadPool排列工作项。我在我的答案中做了最后一个选项。 – rene 2012-08-06 14:42:05

1
repeat: FetchFeed(); 
Thread.Sleep(3600000); 
goto repeat; 

这就是你的问题。 Windows服务应在30秒内响应,因此Windows知道已成功启动。

使用此代码,您将阻止唯一的线程,并且服务不再响应任何事情。

试着用不同的线程或其他方式做这件事。

我建议另一种方式,因为使用goto是一个真正不走:)

还检查了在Windows服务的一些教程和基本的文档,以获得更好的理解。

相关问题