我有以下代码,在Windows服务启动时不执行
我在这里找到了解决方案,但它们对我不起作用。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Configuration;
using System.Threading.Tasks;
namespace TFS_JIRA_sync
{
public partial class SyncProcess : ServiceBase
{
public SyncProcess()
{
InitializeComponent();
}
private System.Timers.Timer timer = new System.Timers.Timer();
protected override void OnStart(string[] args)
{
this.timer.Interval = ScanPeriod.period * 60000; //turn minutes to miliseconds
this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);//OnTimer;
this.timer.Enabled = true;
this.timer.AutoReset = true;
this.timer.Start();
}
private void OnTimer(object sender, System.Timers.ElapsedEventArgs e)
{
Processing proc = new Processing();
proc.doProcess();
}
protected override void OnStop()
{
}
}
}
在程序中:
using System;
using System.Collections.Generic;
using System.ServiceProcess;
using System.Threading.Tasks;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace TFS_JIRA_sync
{
static class Program
{
/// <summary>
/// Главная точка входа для приложения.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new SyncProcess()
};
ServiceBase.Run(ServicesToRun);
//Processing proc = new Processing();
//proc.doProcess();
}
}
}
当我评论部分开始“ServiceBase [] ...”并在Programm类中取消注释“Processing ...”时它工作正常。
但是当我的代码作为Windows服务运行时 - 没有任何事情发生
答案 0 :(得分:0)
将Console.ReadLine()放入代码中:
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new SyncProcess()
};
ServiceBase.Run(ServicesToRun);
Console.ReadLine();
}
答案 1 :(得分:0)