简体   繁体   English

如何捕获服务中的应用程序启动?

[英]How to capture an app start-up in a service?

Try to create a windows service with VS 2012. By default, there are 3 methods for the service: 尝试使用VS 2012创建Windows服务。默认情况下,该服务有3种方法:

Constructor(say MyService)
OnStart
OnStop

but when I put some code in Constructor, looks like it only running one time. 但是当我将一些代码放入Constructor中时,它看起来似乎只运行了一次。

What I want is: when an app started from windows, for example, notepad.exe, I can capture it. 我想要的是:当某个应用程序从Windows启动时,例如notepad.exe,我可以捕获它。 Should I put a dead loop in the constructor to monitor process list? 是否应该在构造函数中放置死循环以监视进程列表? I thought Service should be always on like a dead loop. 我认为Service应该一直处于死循环状态。 So when an app start it, how to capture it in MyService? 因此,当应用启动时,如何在MyService中捕获它?

Should I put a dead loop in the constructor to monitor process list? 是否应该在构造函数中放置死循环以监视进程列表?

No, that goes in OnStart() . 不,这在OnStart() Basic pseudo-code is: 基本的伪代码是:

  • OnStart: OnStart:
    • Set up monitoring 设置监控
    • Start it 启动它
  • Monitoring: 监控:
    • while (true) monitor processes while(true)监视进程
    • if(stop signal) exit 如果(停止信号)退出
  • OnStop 停止
    • Give stop signal to monitoring object 向监控对象发出停止信号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM