Process.Kill () and process events

Here is the situation: I have to create a program that will inject another stream of process output into the text box. This alone will not cause too many problems. However, it does matter that I have to start 5 instances of this console application and redirect the output to 5 text fields, as well as be able to kill any of these processes at any time. As far as I know, the best way to do this is asynchronously. But the problem here is the killing processes that are created on different threads. How to kill him without having access to him, because he does not exist in the area where I have to kill him. My best guess is to get its PID on Process.Start() , so I can kill it, so ...

Is it possible to fire any event from a process using the Process.kill() command? And if not - is there a way to kill the process at about the same time interval as Process.kill() , which fires some kind of event?

Or maybe someone can offer me other approaches or best practices regarding how these problems are usually resolved?

EDIT: The reason I execute all processes on different threads is because I use Thread.Sleep() for some of them, if there is and enter a parameter that tells me that the process should be killed in x seconds.

+4
source share
3 answers
Command

Process.Kill () does for some reason actually execute the process exit event. The easiest way to find out that a process has been killed is to make an unstable line containing information about how it ended. (Change it to "killed" before process.kill, etc.)

+1
source

First of all, you do not need threads. Starting a process is asynchronous in itself, so Process.Start (...) does not block, and you can create as many processes as you want.

Instead of using the static Process.Start method, you should consider creating instances of the Process class and set the CanRaiseEvents property to true. In addition, there are several events that you can register (per instance) - they will only increase if CanRaiseEvents is set to true, but also after exiting / exiting the process (including Kill () calls).

0
source

When you call

 Process.Start() 

it returns an instance of the Process class, which you can use to extract information from it and kill the process at any time

 Process p = Process.Start("process.exe"); //some operations with process, may be in another thread p.Kill() 
0
source

Source: https://habr.com/ru/post/1392640/


All Articles