Click here to Skip to main content
15,671,597 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello every one 
i want to kill specific process by title name coz ppl with this apps can stole my app codes also if i kill it by proccess name they can change the application process name and run it again so i use to kill app by title coz title can't be changed so i use this code
Process[] processes = Process.GetProcesses(); 
foreach (Process process in processes)
                    if (process.MainWindowTitle.Contains("Spy"))
                    if (!process.HasExited) process.Kill();

to kill all proccess with title spy and it's works very good but it's work only for one time so if i use it 1 time i can't use it again i have to close full application and run it again to use it one more time so wat is the problem with code so i can use it on timer and run all time when my app is working and kill the app if started at any time i hope i found help here and thx in advance

What I have tried:

i try to many things but all of them same results still can use the code for only one time
Updated 3-Feb-20 21:15pm

First things first: if you are relying on "windows titles" to identify "rogue processes" then you are in for a massive shock: Titles can be changed at will, and it would be very naive to assume that all rogue programs will contain the word "Spy" in a window title.

Secondly ... a process doesn't have to have a window at all - there are a huge number of processes within windows that have no main window, and thus no Title, and are undetectable by your code.

Thirdly ... Just looking for any three letters in any kind of process info isn't going to help you much - it's horribly likely to cause "false positives" and close innocent processes.

Fourthly ... you can't kill any process - it may be running under a different user, you may not have the required permissions, ...

And finally: the problem you have noticed. We can't help without being able to see what happened when you try - we have no idea how you are calling that code, and that's likely to be important to the whole operation. I'd strongly suggest using the debugger to find out exactly what happens the second time that code is executed, and what has changed since the first run.
Sorry, but we can;t do that for you - time for you to learn a new (and very, very useful) skill: debugging!
Share this answer
Member 13536047 4-Feb-20 3:31am    
exactly i dont know much about debugging that why im looking for easy way can fix my problem right now coz i dont wanna stop my app untill i learn about debugging we can do small fix right now and in the future i can know more that why now i look into this easy way to fix my problem and this code is run only 1 time i try break and return still can't run it again i have to fully close app and run it again .
OriginalGriff 4-Feb-20 3:45am    
We can't give you "a small fix now" because it requires information you can only get by debugging in order to stand a chance of "fixing it".

Debugging is a skill that anyone writing code more complicated than "Hello World" should have developed at least the rudiments of - it's the major part of development, generally far longer than actually writing the code.
And we can't debug it for you: it needs the full code for your app running on a machine under the debugger to start using it (though you can apply the skills without the debugger itself, that takes considerably longer and needs repeated changes to the code, recompile, run again cycles to get what the debugger can tell you in seconds).

So break out the debugger, learn the basics of how to use it from Google, and start looking at what is goin on. Seriously, we can't do that for you (unless you want to pay for flights, hotels, food, booze, and a fairly-huge-but-entirely-reasonable hourly rate?)
Member 13536047 4-Feb-20 3:53am    
it's okay any way thx u alot i will look into my problem more and try to learn :)
OriginalGriff 4-Feb-20 4:12am    
You can execute the task periodically using a timer (see Timer Class (System.Timers) | Microsoft Docs[^]) but... Are you really sure you need it?
Share this answer
Member 13536047 4-Feb-20 3:29am    
i did it by timer but also run 1 time only
CPallini 4-Feb-20 4:05am    
The sample code, in the linked page, shows you how to set up a timer that periodically calls the handler.

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