0

I'm using external exe file in my C# code. please check below code. This code I just find some web site.

string str = @"C:\joseph_Test\external_vendor.exe";

Process myprocess = new Process();
myprocess.StartInfo.FileName = str;
myprocess.Start();

I checked this code is working, and no problem, but next test I need to use another different type of external exe file. and I aware that previous 'myprocess' is still alive. even I restart program, myprocess is still alive. So I search internet that how to kill or terminate existing process. weird thing is that myprocess never terminated.

First, I tried the code below but the process is still alive.

myprocess.WaitForExit();
myprocess.Close();

I used the link below, but it is still not killed.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/50ecbcf2-d2d3-4f21-9775-5b8be1bd4346/how-to-terminate-a-process-in-c?forum=csharpgeneral

Also not terminated with below way.

Kill some processes by .exe file name

I suspect this exe file made by vendor so this happen caused. But the theory is C# create process instance so I believe C# could terminate or kill this process.

Community
  • 1
  • 1
Choi Joseph
  • 197
  • 1
  • 2
  • 12
  • If you are creating the process, you can assign it to a `Job` object, and then end the job. The job will also cause the process to be automatically terminated if your process crashes or closes. – Mitch Aug 14 '15 at 02:40
  • If [kill process](http://stackoverflow.com/questions/3345363/kill-some-processes-by-exe-file-name) you've linked is not able to stop you process (also likely you can't kill it with task manager) it means than you need to figure out what happens (you'd likely need to find someone with Win32 native debugging experience to track that down). With amount of information you've provided that linked question you've found *should* be the answer (and I'm not sure what additional info can make this post non-duplicate). – Alexei Levenkov Aug 14 '15 at 02:47
  • I think Alexei comment is correct. i need to do Win32 native debugging for this. another answer also couldn't kill the process. thanks anyway. ~~~ – Choi Joseph Aug 18 '15 at 02:11

1 Answers1

0

Using the process as you have created:

Process myprocess = new Process();
myprocess.StartInfo.FileName = @"C:\joseph_Test\external_vendor.exe";
myprocess.Start();

In your call to kill the process by name, you can first assure it will be found by not hard coding its name but instead using:

foreach (var process in Process.GetProcessesByName(myprocess.ProcessName))
{
    process.Kill();
}

You can also use your process reference in a event that is fired on application close to combat your issue of it still running after the application has exited.