2017-07-18 26 views
0

我工作的一个Windows服务运行(在Visual Studio 2008中),当我在调试模式下运行,其工作原理:Windows服务并不在释放模式C#

 static void Main() 
    { 
     #if DEBUG 
      Descarga myServicio = new Descarga(); 
      myServicio.OnDebug(); 
      System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite); 
     #else 

      ServiceBase[] ServicesToRun; 
      ServicesToRun = new ServiceBase[] 
      { 
       new Descarga() 
      }; 
      ServiceBase.Run(ServicesToRun); 
     #endif 

    } 

OnDebugStart()只是调用OnStart() Windows服务的方法和运行的线程只是一个无限运行的线程(直到调用OnStop())来检查服务是否仍在运行&在线。

public Timer myTimer; 
    public string fileName; 
    string txtLog; 

    public Descarga() 
    { 
     InitializeComponent(); 
     myTimer = new Timer(); 
     myTimer.Interval = 10000; 
     myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed); 
    } 
    protected override void OnStart(string[] args) 
    { 
     myTimer.Enabled = true;    
    } 

,当我在调试模式下启动这工作完全正常,但是当我在发布运行,该服务被托管,但代码不执行。

我更改了服务的用户,问题仍然存在。

亲切的问候

+0

你使用哪个'Timer'类? –

+0

@ToniWenzel - 从上下文来看,它似乎是['System.Timers.Timer'](https://msdn.microsoft.com/en-us/library/system.timers.timer(v = vs.110)的.aspx)。其他FX Timer类具有不同的属性/回调概念。 –

+0

当你将其作为发行版/服务运行时 - 你是否收到了“服务已启动但停止......”的消息? –

回答

1

从我可以在你的代码中看到,

protected override void OnStart(string[] args) 
{ 
    myTimer.Enabled = true;    
} 

OnStart开始计时。当处于发布模式时,你不会调用它,而我认为驱动服务的计时器没有开始。你需要在发布模式下调用它。