Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to be able to notified of the start of an application and be able to kill that process within milliseconds of it's start, or terminate it if it's already running.

I need to do this from a windows forms c# app. How would I go about this?

Sample code appreciated. I'm at a total loss.

Example:


1. OS is running.
2. I start my forms app ( which somehow moniters for the start of a seperate application somewhere else on the system ) Once that process starts, it needs to be killed. ( doing this on a timer is not feasible, since process could be allowed to run for some time before being killed )
4. one my app is notified of the start of the other application, it should take steps to kill it.


How can I do this?

Windows messages? Hooks? IPC?
Posted
Comments
Sergey Alexandrovich Kryukov 16-Jul-12 18:26pm    
Why? Does not sound like anything legal... :-)
--SA

I would interface to PowerShell. It can get running applications, and return to C#. Can see the following about interfacing to PowerShell: How to run PowerShell scripts from C#[^]
 
Share this answer
 
The whole idea means you are doing something bad. No, I don't mean that you are writing malicious software. If this is your application, you can always program it to terminate properly; and if your want to just kill it synchronously by some external process, this is extremely unsafe, bad for support, etc.

Anyway, you can handle the process creation events with WMI. Please see this CodeProject article:
Process Information and Notifications using WMI[^].

Does it worth it? No, by the reasons I explained above. I don't think it even worth discussing. Again, it its your application, even it you want to cause its termination by external process, you can always program it to communicate with such process and synchronize the termination with its internal state. So, the communication means IPC, and the best way to organize such IPC would be some remoting, either "classical" .NET remoting or self-hosted WCF. As the problem is not tricky at all, it can be one or another.

Well, is it much better? Yes and no. At least doing so you would not commit a crime you are about to commit right now. But the whole idea of controlling the lifetime of one process by some other process… on the same machine… I have no idea of the architecture you keep in mind, but I would totally rethink it. Don't do evil.

More importantly, your approach to asking questions is also wrong. You just ask how to implement some technical effect, unreasonably assuming that what you want to do is right. You should start the question with explanation of your ultimate goal. Without it, going in further detail just would not make sense; the possible solutions would depend on those goals.

—SA
 
Share this answer
 
Sergey,

I think you are misunderstanding my intent a little bit. What I want to do is to be able to monitor for and actively terminate a process if it even tries to start on the system. No malicious intent involved by any means :-) Just more an anti-spyware app I'm developing.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900