我正在构建一个应用程序监控服务来写入事件日志,这样我就可以大致了解计算机的使用频率。我可以安装该服务,当我尝试运行它时出现此错误:
“本地计算机上的 Application Monitor 服务启动然后停止。某些服务在没有工作时会自动停止,例如性能日志和警报服务。”
我已经尝试了 while 循环、鼠标事件等...但它一直在结束。关于我能做什么的任何想法?
public ApplicationMonitor()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("Activity Usage"))
{
System.Diagnostics.EventLog.CreateEventSource("Activity Usage", "Computer Log");
}
eventLog1.Source = "Activity Usage";
eventLog1.Log = "Computer Log";
}
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("In OnStart");
}
protected override void OnStop()
{
eventLog1.WriteEntry("In onStop.");
}
protected override void OnContinue()
{
eventLog1.WriteEntry("In OnContinue.");
}
最佳答案
因为没有事件线程。
您需要在服务的“OnStart”方法中启动执行轮询的线程。否则,当 OnStart 方法完成时,应用程序中没有事件线程,.Net CLR 将关闭应用程序。
即使线程只是每 30 秒进行一次线程休眠,它也必须处于事件状态才能使应用保持打开状态。
关于c# - 服务应用程序将不会保持运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11996679/