进程可执行文件启动的 .NET 事件
有没有办法注册特定文件名的可执行文件启动时触发的事件? 我知道通过获取进程句柄并注册退出事件,在进程退出时获取事件很容易。 但是,当尚未运行的进程启动时,如何才能收到通知……而不需要轮询所有正在运行的进程呢?
Is there any way to register for an event that fires when an executable of a particular filename starts? I know it's easy enough to get an event when a process exits, by getting the process handle and registering for the exited event. But how can you be notified when a process, that isn't already running, starts...without polling all the running processes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是代码。
请注意,您必须像管理员一样启动 Visual Studio 才能执行此代码。
Here is code.
Notice that you have to start Visual Studio like Administrator in order to execute this code.
您可以使用以下命令:
然后,您可以调用 WatchForProcessStart 和/或 WatchForProcessEnd 并传入您的进程名称(例如“notepad.exe”)。
ManagementEventWatcher 对象是从两个 Watch* 方法返回的,因为它实现了 IDisposable,因此您应该在使用完这些对象后对它们调用 Dispose,以防止出现问题。
如果您需要在流程开始后更快地引发事件,您还可以更改查询中的轮询值。 为此,请将“WITHIN 10”行更改为小于 10 的值。
You could use the following:
You would then call either WatchForProcessStart and/or WatchForProcessEnd passing in your process name (eg "notepad.exe").
The ManagementEventWatcher object is returned from the two Watch* methods as it implements IDisposable and so you should call Dispose on these objects when you have finished with them to prevent issues.
You could also change the polling value in the queries if you need the event to be raised more quickly after the process has started. To do this change the line "WITHIN 10" to be WITHIN something less than 10.
WMI 可以在创建进程时创建事件。 然后您可以过滤这些事件。
WMI can create events when processes are created. You could then filter these events.