【问题标题】:Windows Service does not execute in release mode C#Windows 服务不在发布模式下执行 C#
【发布时间】:2017-07-18 11:07:05
【问题描述】:

我正在开发一个 Windows 服务(在 Visual Studio 2008 中),当我在调试模式下运行它时它可以工作:

        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() 只是调用 Windows 服务的OnStart() 方法,而运行的线程只是一个无限运行的线程(直到调用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;            
    }

当我在调试模式下启动它时,它工作得非常好,但是当我在发行版中运行它时,服务被托管,但代码不执行。

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

亲切的问候

【问题讨论】:

  • 你使用哪个 Timer 类?
  • @ToniWenzel - 从上下文来看,它似乎是System.Timers.Timer。其他 FX Timer 类具有不同的属性/回调概念。
  • 当您将其作为发布/服务运行时 - 您是否收到类似于“服务已启动但随后停止...”的消息?

标签: c# visual-studio visual-studio-2012


【解决方案1】:

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

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

您的OnStart 正在启动计时器。在发布模式下,您没有调用它,并且我认为正在驱动服务的计时器没有启动。您还需要在发布模式下调用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    相关资源
    最近更新 更多