[英]how to kill c++ process using c++ and let it do something before hte kill
I am trying to control a process that is written using c++ with a windows C# user interface 我正在尝试控制使用C ++和Windows C#用户界面编写的过程
first I run the process 首先,我运行该过程
private void button1_Click(object sender, EventArgs e)
{
Process myProcess = new Process();
myProcess.StartInfo.FileName = "filepath.exe";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
}
This will run the process 这将运行过程
Now after I run it for a while I would like to terminate it using: 现在,在运行了一段时间之后,我想使用以下命令终止它:
process.Kill();
However, like the Ping
command I would like it to generate some result, write them to a file then kill 但是,像
Ping
命令一样,我希望它生成一些结果,将它们写入文件然后杀死
So is there a way to find out if another process is trying to kill this process so I lead it to the write file function 那么有没有办法找出另一个进程是否正在试图杀死该进程,所以我将其引向写入文件功能
whenever you kill a process you are actually sending it a signal (SIGKILL most likely), so all you need to do is to assign a signal handler for that particular signal, or you can have the same one for several: 每当您杀死一个进程时,您实际上是在向它发送信号(很可能是SIGKILL),因此您所需要做的就是为该特定信号分配一个信号处理程序,或者您可以为多个信号使用相同的信号处理程序:
signal(SIGTERM, &terminateSigHandler);
signal(SIGKILL, &terminateSigHandler);
etc..
As @Alex Farber mentioned in his comment, there is no way for Process.Kill
to execute anything before killing the program. 正如@Alex Farber在评论中提到的那样,
Process.Kill
在终止程序之前无法执行任何操作。
In this situation I would personally write a function that executes whatever, then kills the process. 在这种情况下,我会亲自编写一个函数,该函数执行所有操作, 然后终止该进程。
Example: 例:
private void killProcess()
{
[execute code here prior to killing the process]
process.Kill();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.