user3719528
user3719528

Reputation: 1

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

Upvotes: 0

Views: 1116

Answers (2)

Pandrei
Pandrei

Reputation: 4951

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..

Upvotes: 1

AStopher
AStopher

Reputation: 4540

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

Upvotes: 0

Related Questions