我创建了一个 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());
}
}
上面的代码用于服务 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);
}
}
}
如您所见,我已经编写了代码以在初始化或启动时登录..但是没有记录。
编辑:
定时器代码(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";
}
谢谢