【发布时间】:2017-08-15 15:04:23
【问题描述】:
长话短说,我构建了一个运行良好的控制台应用程序,然后被告知它需要是一个 Windows 服务。因此,我几乎按照 Microsoft 的建议构建了一个 Windows 服务项目,该服务可以正常运行、启动和停止,但它不会启动我从控制台应用程序转换为服务的应用程序。我很难理解为什么。
问题: 如何使用 Windows 服务启动长时间运行的进程? windows服务不能使用类库中的代码吗?
public partial class CallQService : ServiceBase
{
private static System.Threading.Timer _timer;
private static CallQ _callQ;
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool SetServiceStatus(IntPtr handle, ref ServiceStatus serviceStatus);
public CallQService(string[] args)
{
// Update the service state to Start Pending.
ServiceStatus serviceStatus = new ServiceStatus();
serviceStatus.dwCurrentState = ServiceState.SERVICE_START_PENDING;
serviceStatus.dwWaitHint = 100000;
SetServiceStatus(ServiceHandle, ref serviceStatus);
InitializeComponent();
string eventSourceName = "MySource";
string logName = "MyNewLog";
if (args.Any())
{
eventSourceName = args[0];
}
if (args.Length > 1)
{
logName = args[1];
}
eventLog1 = new EventLog();
if (!SourceExists(eventSourceName))
{
CreateEventSource(eventSourceName, logName);
}
eventLog1.Source = eventSourceName;
eventLog1.Log = logName;
// Update the service state to Running.
serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING;
SetServiceStatus(ServiceHandle, ref serviceStatus);
}
protected override void OnStart(string[] args)
{
Debugger.Launch();
eventLog1.WriteEntry("The Service Has Started");
System.Timers.Timer timer = new System.Timers.Timer
{
Interval = 60000 // 60 seconds
};
timer.Elapsed += new ElapsedEventHandler(OnTimer);
timer.Start();
//This is where the process starts that is supposed to run my application and generate my data. It does not do that at all. it hits this and runs through the code in this class file, but once it is supposed to move into code in one of my class library's it just stops and doesn't do anything else.
Initialize();
}
private void OnTimer(object sender, ElapsedEventArgs e)
{
// TODO: Insert monitoring activities here.
eventLog1.WriteEntry("Monitoring the System", EventLogEntryType.Information);
}
protected override void OnContinue()
{
eventLog1.WriteEntry("In OnContinue.");
// Update the service state to Running.
ServiceStatus serviceStatus = new ServiceStatus();
serviceStatus.dwCurrentState = ServiceState.SERVICE_CONTINUE_PENDING;
serviceStatus.dwWaitHint = 100000;
SetServiceStatus(ServiceHandle, ref serviceStatus);
serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING;
SetServiceStatus(this.ServiceHandle, ref serviceStatus);
}
protected override void OnPause()
{
// Update the service state to Start Pending.
ServiceStatus serviceStatus = new ServiceStatus();
serviceStatus.dwCurrentState = ServiceState.SERVICE_PAUSE_PENDING;
serviceStatus.dwWaitHint = 100000;
SetServiceStatus(ServiceHandle, ref serviceStatus);
eventLog1.WriteEntry("The Service has Stopped.");
// Update the service state to Running.
serviceStatus.dwCurrentState = ServiceState.SERVICE_PAUSED;
SetServiceStatus(this.ServiceHandle, ref serviceStatus);
}
protected override void OnStop()
{
// Update the service state to Start Pending.
ServiceStatus serviceStatus = new ServiceStatus();
serviceStatus.dwCurrentState = ServiceState.SERVICE_STOP_PENDING;
serviceStatus.dwWaitHint = 100000;
SetServiceStatus(ServiceHandle, ref serviceStatus);
eventLog1.WriteEntry("The Service has Stopped.");
// Update the service state to Running.
serviceStatus.dwCurrentState = ServiceState.SERVICE_STOPPED;
SetServiceStatus(this.ServiceHandle, ref serviceStatus);
}
public static void Initialize()
{
var callDataRepo = CreateCallDataRepo();
var skillsRepo = CreateSkillsRepo();
_callQ = new CallQ(skillsRepo, callDataRepo);
_callQ.Start();
SetUpTimer(new TimeSpan(06, 46, 00));
}
private static ISkillsRepo CreateSkillsRepo()
{
// Generates an instance of Config data for pulling down skills from OADB database
ISkillsDataRepoConfig skillDataRepoConfig = new SkillsDataRepoConfig();
// generates an instance of skills repo and pulling in call data repo OADB databse config
ISkillsRepo skillsRepo = new SkillsRepo(skillDataRepoConfig);
return skillsRepo;
}
private static ICallDataRepo CreateCallDataRepo()
{
// Generates an instance of Config data for pulling down skills from OADB database
ICallDataRepoConfig callDataRepoConfig = new CallDataRepoConfig();
// generates an instance of call data repo and pulling in call data repo OADB databse config
ICallDataRepo callDataRepo = new CallDataRepo(callDataRepoConfig);
return callDataRepo;
}
private static void SetUpTimer(TimeSpan alertTime)
{
DateTime current = DateTime.Now;
TimeSpan timeToGo = alertTime - current.TimeOfDay;
if (timeToGo < TimeSpan.Zero)
{
return;//time already passed
}
_timer = new System.Threading.Timer(x =>
{
_callQ.StopGenerators();
_callQ.Start();
}, null, timeToGo, Timeout.InfiniteTimeSpan);
}
}
【问题讨论】:
-
您是否尝试将初始化方法中存在的代码移动到 OnStart 方法并查看应用程序是否正在启动?
-
检查 Windows 错误日志。有时会出现运行时错误,导致服务无法运行。这些错误通常会记录在操作系统错误日志中。
-
@OwenPauling 我会尝试为此创建一个后台线程,看看是否有帮助。我确实想指出,与其他帖子中发生的情况不同,我的服务不会停止运行。
-
@PankajKapare 将 initialize 中的代码移动到 onstart 不会改变任何内容
-
@Sparrow 没有报告错误。它每分钟都报告为我设置的计时器,但它们没有错误。
标签: c# service windows-services