简体   繁体   中英

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

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

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:

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.

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM