I want to make in my program the function of tracking processes and closing the program when they are detected, namely sniffer packages that track sent requests. I know what to do is useless, but from the lamers such a simple defense would be fine with me.

Sketched such a simple code:

static void KillThemAll() { while (true) { if ((Process.GetProcessesByName("FirstProgram").Length > 0 || Process.GetProcessesByName("SecondProgram").Length > 0 || Process.GetProcessesByName("ThirdProgram").Length > 0) || Environment.HasShutdownStarted || FindWindow("", "FourProgram") != IntPtr.Zero || FindWindow("", "FiveProgram") != IntPtr.Zero) { Environment.Exit(0); } Thread.Sleep(1000); } } 

Further after InitializeComponent(); I use my KillThemAll(); function KillThemAll(); and trying to run a program. Closing processes is fine, but the interface does not appear. Apparently due to while(true) my code becomes unreachable. But how to fix it and make my ideas work? I hope for your suggestions.

  • Your code without registration, it is impossible to read. - tym32167
  • @ tym32167 I apologize, did not immediately figure out how to make out. Is everything okay now? - S3RL
  • Yes, now everything is clear. Either run this code in a separate thread, or rewrite it for asynchronous execution, or both. Now this code running in the UI thread does not allow your interface to do anything. - tym32167
  • Also note that you don’t use the Process p process inside the loop. So do you need this cycle at all? - tym32167
  • @ tym32167 Thanks for the help, rewrote the code. - S3RL pm

1 answer 1

The Environment.Exit () method is universal, in the sense that it can terminate an application from any thread. It is enough in program.cs to call Application.Run (...); or after InitializeComponent, create a thread or task and start a loop in it like this Task.Run (() => SnifferDefender ()); then the loop will work in another thread and the main program thread will continue execution

  • Is this exactly the answer to the question? - aleksandr barakin 4:47 pm
  • Yes, exactly what the author of the question wants to achieve - Berianidze Luka
  • @aleksandrbarakin Yes, that's exactly what I wanted. - S3RL pm
  • @Lukacho Thank you very much for your help, everything turned out) - S3RL pm