【问题标题】:installed windows service but not working已安装 Windows 服务但无法正常工作
【发布时间】:2012-04-20 06:19:55
【问题描述】:

我创建了一个 Windows 服务并为其创建了安装程序。它已安装并启动,但我在其中编写的代码没有执行。实际上,当我从服务窗口启动服务时,不会触发 OnStart() 函数。也不是 initializecomponent() 也不是 static void main 函数..任何人都可以帮助我

请指导我哪里做错了。

这里有一些代码行。如果你想要更多我写的东西,请告诉我

public partial class iMArchiveService : ServiceBase
{
    Boolean isArchiving = false;

    public iMArchiveService()
    {
        MyException.CreateLog("iMArchiveService: Inside Constructor. Initializing Component");
        InitializeComponent();
        MyException.CreateLog("iMArchiveService: Component Initialized. Timer is set as: " + TimeMachine.Interval.ToString() + " milliseconds");
    }

    protected void TimeMachine_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        try
        {
            MyException.CreateLog("iMArchiveService: Inside Tick Try. Value of isArchiving variable before condition is: " + isArchiving.ToString());
            if (!isArchiving)
                isArchiving = new iM.OrderArchiving.ArchiveOrderXML().ArchiveOrderService();
            MyException.CreateLog("iMArchiveService: Inside Tick Try. Value of isArchiving variable after condition is: " + isArchiving.ToString());
        }
        catch (Exception ex)
        {
            MyException.CreateLog("iMArchiveService: Inside Tick Catch :(");
        }
    }

    protected override void OnStart(string[] args)
    {
        TimeMachine.Enabled = true;
        MyException.CreateLog("iMArchiveService Started: " + DateTime.Now.ToString());
    }

    protected override void OnStop()
    {
        TimeMachine.Enabled = false;
        MyException.CreateLog("iMArchiveService Stopped: " + DateTime.Now.ToString());
    }

}

上面的代码是服务文件.cs

这是我的项目安装文件

namespace iM.OrderArchivingService
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
        public ProjectInstaller()
        {
        InitializeComponent();
        }
    }
}

这里是 InitializeComponenet 函数 -

private void InitializeComponent()
    {
        this.myServiceProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
        this.myServiceInstaller = new System.ServiceProcess.ServiceInstaller();
        // 
        // myServiceProcessInstaller
        // 
        this.myServiceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
        this.myServiceProcessInstaller.Installers.AddRange(new System.Configuration.Install.Installer[] {
        this.myServiceInstaller});
        this.myServiceProcessInstaller.Password = null;
        this.myServiceProcessInstaller.Username = null;
        // 
        // myServiceInstaller
        // 
        this.myServiceInstaller.ServiceName = "iMArchiveService";
        // 
        // ProjectInstaller
        // 
        this.Installers.AddRange(new System.Configuration.Install.Installer[] {
        this.myServiceProcessInstaller});

    }

这是 program.cs 文件

namespace iM.OrderArchivingService
{
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main(string[] args)
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] { new iMArchiveService() };
        ServiceBase.Run(ServicesToRun);
    }
}
}

正如你所见,我已经编写了在初始化或启动时记录的代码。但没有记录。

编辑:

定时器代码(TimeMachine)

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.TimeMachine = new System.Timers.Timer(3600000);
        // 
        // TimeMachine
        // 
        this.TimeMachine.Interval = 3600000;
        this.TimeMachine.Elapsed += new System.Timers.ElapsedEventHandler(TimeMachine_Elapsed);
        // 
        // iMArchiveService
        // 
        this.ServiceName = "iMArchiveService";

    }

thnx

【问题讨论】:

  • TimeMachine定时器的间隔是多少?
  • 添加了计时器代码..但至少它应该写我在开始时编码的日志。日志也不是为此而写的
  • 我猜这不是计时器问题...日志进一步显示它到达了 projectinstaller 初始化组件功能,但它没有得到必须启动的东西。我的意思是我的初始化函数需要引用我的服务基类...

标签: c# .net service windows-services installation


【解决方案1】:

您使用了错误的 Timer 类 - 线索在其命名空间中:System.Windows.Forms.Timer。该计时器仅适用于 WinForms 应用程序。

您应该改用System.Timers.Timer


System.Threading.Timer中有关于定时器类的一般性讨论:

System.Threading.Timer 是一个简单的轻量级计时器,它使用回调方法并由线程池线程提供服务。不建议与 Windows 窗体一起使用,因为它的回调不会发生在用户界面线程上。 System.Windows.Forms.Timer 是与 Windows 窗体 一起使用的更好选择。对于基于服务器的计时器功能,您可以考虑使用System.Timers.Timer,它会引发事件并具有附加功能。

(我的重点取代了原来的)

【讨论】:

    【解决方案2】:

    不要在 Windows 服务中使用System.Windows.Forms.Timer,它可能不会在其中引发事件。见The Windows Forms Timer event is not raised in a Windows service

    在 Windows 服务中使用 System.Timers.TimerSystem.Threading.Timer。见Windows service and timer

    【讨论】:

    • 它不是计时器问题.. 因为它必须达到服务。但这仍然会导致以后出现问题,因此请修复它,谢谢
    【解决方案3】:

    请检查 TimeMachine.Enable = True 是否设置了计时器的计时。

    参考这个链接

    http://codesimplified.com/2010/12/31/creating-window-service-with-net-vs2008/

    【讨论】:

      【解决方案4】:

      也许您应该使用 Timer.Start() 和 Timer.Stop() 方法来启动和停止计时器,以防万一使用 Enabled 属性时出现问题。

      间隔时间为 3,600,000,即 3600 秒或 60 分钟 = 1 小时。一个小时过去了,什么都不会发生;这是你的本意?

      顺便说一句,像下面的例子那样设置间隔会让你的代码更容易阅读:

      this.TimeMachine.Interval = 1 * 1000;  // 1 Second
      this.TimeMachine.Interval = 60 * 1000; // 60 Seconds
      this.TimeMachine.Interval = 60 * 60 * 1000; // 1 hour
      

      尝试使用 System.Diagnostics 中的 Debug.Writeline() 方法。默认情况下,它将在 MSVS 的输出窗口中发布消息。您还会在那里看到任何例外情况。

      【讨论】:

        猜你喜欢
        • 2011-12-18
        • 2017-10-05
        • 2015-02-05
        • 2015-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多