简体   繁体   中英

React to Process termination

I'm writing an application in C# and for security reasons it is important for me to know when its process(Process A) has been killed or closed.

My first thought is to make another process (Process B) watch Process A and react accordingly if this process no longer exists. The problem is obvious, though: Closing Process B will make it possible to close Process A.

This is probably a (kind of)common situation and as such, there is likely already a solution for it. Unfortunately I wasn't able to find one.

What are common ways to achieve this?

You can make use of the Process.Exited Event. By subscribing to this event, you can have a callback which will be called when the process has exited or has been terminated.

There is a good example on how to use it in the documentation: https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.exited?view=netcore-3.1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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