有没有办法检测 C# .Net 2.0 中一个进程是否/何时启动另一个进程?
我正在寻找一种方法来检测我的应用程序启动的进程是否又产生了其他进程。希望我可以有一个线程启动和应用程序,监视直到它退出,然后在该应用程序退出时触发一些其他操作。问题是,对于我想要监视的某些应用程序,它们正在启动一个附加进程,然后退出。所以我的触发器在应用程序退出之前触发。
例如,我在进程中启动一个程序。该可执行文件会进行一些文件修补,然后启动主程序(一个单独的 exe),然后退出。这会导致 Process.HasExited 返回 true,从而过早地触发我的触发器。我真正关心的是主程序何时退出,在本例中是初始进程启动的程序,但我似乎找不到一种以编程方式执行此操作的方法。
I'm looking for a way to detect if a Process started by my application has in turn spawned additional Processes. The hope is that i can have a thread that starts and application, watches until it exits, and then triggers some other action when that application exits. The problem is that with some of the applications that i want to monitor, they are starting an additional process and then exiting. so my trigger fires before the application has exited.
for example, i start a program in a Process. that executable does some file patching, and then launches the main program - a separate exe - and then exits. this causes Process.HasExited to return true, which fires off my trigger prematurely. what i really care about is when the main program exits, in this case the program that the initial Process started, but i can't seem to find a way to do this programatically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我明白了。使用 .Net 2.0 中的 Process 类无法直接访问父进程信息,而是使用 System.Diagnostics.PerformanceCounter 类,您可以从“创建进程 ID”计数器名称访问父进程 ID。为了解决我的问题,我做了类似的事情:
我在此示例中省略了错误处理,作为更高级的解决方案,您可以通过维护所有分支进程 ID 的列表来跟踪生成的进程树,此示例仅监视一个直接子进程。
ok, i figured it out. there's no direct access to parent process information using the Process class in .Net 2.0, but using the System.Diagnostics.PerformanceCounter class you can get access to the parent process id from the "Creating Process Id" counter name. to solve my problem i did something similar to:
i left out error handling in this example, and as a more advanced solution, you could keep track of a tree of spawned processes by maintaining a list of all branch process ids, this example only watches one direct child process.
如果这些其他进程是专有的/不在您的控制范围内,我真的不知道如何监视它们,检查它们是否生成新进程,更重要的是,如果您想检查生成的进程是否生成孙子进程并且......(你知道我要去哪里)。
无论如何,这是我的观点,它可能是可行的,我只是不认为单独使用 .NET/C# 可以实现它。
希望其他人能够提供真正的解决方案。
If those other processes are proprietary/not within your control, I really don't see how you can monitor them the check if they spawned new processes, more so if you want to check if the spawned processes spawn grandchildren processes and... (you know where I'm going).
Anyway that's my opinion, it might be do-able, I just don't think it can be achieve using .NET/C# alone.
Hopefully someone else can provide a real solution.
我认为,这并不是非常简单,但是可行。
在这种情况下,我会选择使用 WMI。你先开始
枚举现有进程 [ether way、WMI 或 Net Process] 并记住这些,您将看到。
然后启动两个 WMI 事件查询,其中一个使用 Win32_ProcessStartTrace
另一个用于查询 Win32_ProcessStopTrace [请参阅 WMI SDK]。
两者都返回一个 Win32_Process 实例,其中始终包含
“父进程ID”。如果这是其中之一,您之前保存过,
你有衍生的进程。 “Win32_Process”还包含
一个“杀死”方法;不知道你最终想做什么;-)
希望,这会有所帮助。请随时询问更多详情。
br--马布拉
I think, it's not really very simple, but doable.
In this case, I would opt for using WMI instead. You first start
enumerating the existing processes [ether way, WMI or Net Process] and remembers these, you'll watch.
Then you start two WMI eventqueries, one using Win32_ProcessStartTrace
and onther one to query Win32_ProcessStopTrace [see WMI SDK].
Both return an instance of Win32_Process, which always contains
the "ParentProcessID". If this is one of these, you saved before,
you have the spawned process. The "Win32_Process" also contains
a "Kill" method;No clue, what you finally want to do ;-)
Hope, this helps. Don't hesitate to ask for more details.
br--mabra