C# 已安装windows服务但不工作
我创建了一个windows服务并为其创建了安装程序。它已安装并启动,但我在其中编写的代码没有执行。实际上,当我从服务窗口启动服务时,OnStart()函数并没有被触发。也不是initializecomponent()或静态void主函数。。有人能帮我吗 请告诉我哪里做错了 下面是一些代码行。如果你想要更多我写的东西,请告诉我C# 已安装windows服务但不工作,c#,.net,service,windows-services,installation,C#,.net,Service,Windows Services,Installation,我创建了一个windows服务并为其创建了安装程序。它已安装并启动,但我在其中编写的代码没有执行。实际上,当我从服务窗口启动服务时,OnStart()函数并没有被触发。也不是initializecomponent()或静态void主函数。。有人能帮我吗 请告诉我哪里做错了 下面是一些代码行。如果你想要更多我写的东西,请告诉我 public partial class iMArchiveService : ServiceBase { Boolean isArchiving = false;
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());
}
}
上面的代码用于service 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);
}
}
}
thnx请检查TimeMachine.Enable=True,是否设置了计时器的计时 请参阅此链接
您使用了错误的
Timer
类-线索在其命名空间中:System.Windows.Forms.Timer
。该计时器仅在WinForms应用程序中有效
相反,您应该切换到使用
在以下章节中对计时器类进行了一般性讨论:
System.Threading.Timer
是一个简单、轻量级的计时器,它使用回调方法,由线程池线程提供服务。不建议将其用于Windows窗体,因为其回调不会发生在用户界面线程上System.Windows.Forms.Timer
是与Windows窗体一起使用的更好选择。对于<强>基于服务器的计时器功能>,您可以考虑使用<代码> Stase.Time.Time<代码>,这会引发事件并具有附加的特性。
(我的重点取代了原来的内容)不要在Windows服务中使用
System.Windows.Forms.Timer
,它可能不会在其中引发事件。看
在windows服务中使用
System.Timers.Timer
或System.Threading.Timer
。请参阅。也许您应该使用Timer.Start()和Timer.Stop()方法来启动和停止计时器,以防使用Enabled属性时出现问题
间隔时间为3600000,即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()方法。默认情况下,将在MSV的输出窗口中发布消息。您也会看到任何异常。计时器计时器的间隔是多少?添加了计时器代码。。但至少它应该写我在开始时编写的日志。日志也不是为此而写的,我想这不是计时器的问题。。。日志进一步显示,它到达了projectinstaller initialize componenet函数,但没有得到必须启动的内容。我的意思是我的初始化函数需要有一个对我的servicebase类的引用…这不是计时器的问题。。因为要做到这一点,它必须提供服务。但这仍然会在以后导致问题,所以请修复它,谢谢
this.TimeMachine.Interval = 1 * 1000; // 1 Second
this.TimeMachine.Interval = 60 * 1000; // 60 Seconds
this.TimeMachine.Interval = 60 * 60 * 1000; // 1 hour