如何从我的 windows 服务中监听进程启动事件?
How to listen for a process start event from my windows service?
我希望我的 Windows 服务能够侦听特定进程(例如进程 A)何时运行并能够随意将其关闭。这怎么可能?
您可以使用 ManagementEventWatcher class 在进程启动时接收通知。
Subscribes to temporary event notifications based on a specified event query.
MSDN 页面上有一个示例可能适合您的需要。
The following example shows how the client receives notification when an instance of Win32_Process is created because the event class is __InstanceCreationEvent.
ManagementEventWatcher
class 允许您设置 WqlEventQuery to specify the type of WQL event to listen for. More information on WQL query options and structure are available in the WQL (SQL for WMI) docs and WMI Reference.
或者,您可以使用计时器按一定时间间隔查询由 Process.GetProcesses() 返回的 运行 个进程数组。
然后,使用 Process.GetProcessByName() 或直接从 Process.GetProcesses() 返回的结果数组中获取对流程的引用,并使用 Process.Kill()方法杀死进程。
我希望我的 Windows 服务能够侦听特定进程(例如进程 A)何时运行并能够随意将其关闭。这怎么可能?
您可以使用 ManagementEventWatcher class 在进程启动时接收通知。
Subscribes to temporary event notifications based on a specified event query.
MSDN 页面上有一个示例可能适合您的需要。
The following example shows how the client receives notification when an instance of Win32_Process is created because the event class is __InstanceCreationEvent.
ManagementEventWatcher
class 允许您设置 WqlEventQuery to specify the type of WQL event to listen for. More information on WQL query options and structure are available in the WQL (SQL for WMI) docs and WMI Reference.
或者,您可以使用计时器按一定时间间隔查询由 Process.GetProcesses() 返回的 运行 个进程数组。
然后,使用 Process.GetProcessByName() 或直接从 Process.GetProcesses() 返回的结果数组中获取对流程的引用,并使用 Process.Kill()方法杀死进程。