2012-04-20 208 views
0

我已经创建了一个windows服务并为其创建了安装程序。它已安装并启动,但我写入其中的代码未执行。实际上,当我从服务窗口启动服务时,OnStart()函数未被触发。也没有初始化组件(),也没有静态无效的主要功能..任何一个可以帮助我吧安装了windows服务但不工作

请指导我在哪里做错了。

这里有一些代码行。让我知道如果u想要更多的东西我已经写

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()); 
    } 

} 

上面的代码是服务file.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); 
    } 
} 
} 

因为你看我已经写了代码登录时初始化或开始..但没有日志正在作出。

编辑:

代码定时器(时间机器)

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

+0

TimeMachine计时器的间隔是多少? – Coder 2012-04-20 06:23:46

+0

添加了计时器代码..但至少它应该写入我开始编码的日志。该日志也不写为 – 1Mayur 2012-04-20 07:05:48

+0

我猜它不是计时器问题...日志记录进一步显示它到达projectinstaller初始化组件函数,但它没有得到什么必须开始。我的意思是我的初始化函数需要引用我的服务基类... – 1Mayur 2012-04-20 09:35:56

回答

1

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

你应该,而是改用System.Timers.Timer


还有就是定时器类的System.Threading.Timer了一般性讨论:

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

(我强调代替原件)

+0

让我们试试这种方式 – 1Mayur 2012-04-20 08:09:33

+0

试过..仍然没有成功 – 1Mayur 2012-04-20 08:26:28

0

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

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

BTW,设置像下面的例子中的间隔将使你的代码变得更容易阅读:

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

尝试使用中的Debug.WriteLine()方法在System.Diagnostics程序。默认情况下,将在MSVS的输出窗口中发布消息。你也会看到有任何异常。