0

I have program called gen.exe, which prints 1 to ..... It just keeps prints and saves them in file.

Now, in my .net application I'm using the following code to create a process and to run the program :

process = new Process
                {
                    StartInfo =
                    {
                        FileName = "gen"//defined in filepath or in the same directory 
                    }
                };
                process.Start();

Now I want to terminate this process. But when I use kill, it closes the console and some numbers printed at very last doesn't appear on the file and sometimes it says file is corrupted.

All I want is a simple cancellation of the process, just like pressing CTRL + C.

How can I achieve this ?

I have tried Dispose, Close, Kill.

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
  • Is this a program that you created / can alter? If so the “easiest” way is probably to make it controllable via piped input. (“CTRL+C/Break” is a very rude way to close a process and may result in corrupt state unless the process goes through special mechanisms of adding handlers. However, unlike a standard kill..) – user2864740 Jul 11 '20 at 21:30
  • There is a whole article by MS about IPC: https://learn.microsoft.com/en-us/windows/win32/ipc/interprocess-communications | But with a console programm, you can add I/O Redirection to the list. – Christopher Jul 11 '20 at 21:31
  • @user2864740 I created that program, I can alter that.. **But I can alter my original task**, I'm working on top of ffmpeg, so if I **have to** emulate *CTRL + C* – Maifee Ul Asad Jul 11 '20 at 21:32
  • @OlivierRogier I want to interrupt gen.exe, after gen.exe has been terminated, I will simply kill the process – Maifee Ul Asad Jul 11 '20 at 21:33
  • https://stackoverflow.com/q/813086/2864740 might provide some useful links and information. – user2864740 Jul 11 '20 at 21:33
  • I think the OP is looking for a replacement method of “Process.Close” (which is a _direct and immediate termination_, bypassing all console handlers) that will trigger the external apps handling of the console CTRL registration, such that the external app can do a “graceful” termination including flushing working buffers. – user2864740 Jul 11 '20 at 21:36
  • I agree with @OlivierRogier. "I want to interrupt gen.exe, after gen.exe has been terminated, I will simply kill the process" Interrupting is a question of IPC. However you can not kill aprocess that has terminated. Killing is a way to **get** a process to the state "terminated" from the outside – Christopher Jul 11 '20 at 21:36
  • actually there is two process, one is created by `new Process`, other by `FileName = "gen"`.. as far as i'm observing.. first i want to interrupt that gen.exe, when it finishes, then I will kill the console I create by `new Process` @OlivierRogier .. this is my plan so far.. – Maifee Ul Asad Jul 11 '20 at 21:36
  • @MaifeeUlAsad Nothing in your problem or your description makes sense. It sounds like what you *want* is cto tell gen.exe to terminate. And then do something once it did. Telling gen.exe that is IPC. Waiting for the shutdown, is `WaitForExit()` (but that needs some multitasking) or the `Exited` Event https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.waitforexit | I think you need to go 3 steps back. It sounds like you are on the 2nd layer of a XY Problem. – Christopher Jul 11 '20 at 21:40
  • AFAIK, `Kill()` cause `Ctrl+C`. – aepot Jul 12 '20 at 11:38
  • hello everyone [here](https://stackoverflow.com/a/29274238/10305444) is a thread that I found interesting and [it actually worked](https://github.com/maifeeulasad/Cream/blob/master/Cream/Cream/Cream.cs#L22-L33).. thank you everyone.. I'm learning IPC, but for this instance I didn't need that complex communication.. tell me your thought.. – Maifee Ul Asad Jul 19 '20 at 07:47

0 Answers0