开发者

killing process java.exe

开发者 https://www.devze.com 2022-12-13 01:48 出处:网络
I launch some batch file from my C# app via Process class. This file started java app. I cant recognize开发者_StackOverflow社区 which process called java.exemust be kill to stop this application. Is t

I launch some batch file from my C# app via Process class. This file started java app. I cant recognize开发者_StackOverflow社区 which process called java.exe must be kill to stop this application. Is there some more elegant way than kill them all, hope that i end the right one and dont kill some other important java app? Thanks for any help I really stuck there.

Cheers, Andrew


EDIT: I missed the batch file part. This answer will only be useful if you can launch the process directly from C#...

When you start the process you can store the process Id so you can get the process back later and kill it.

Process p = new Process();
//set all the process start Info...
p.Start();

someVariableICanGetToLater = p.Id

//Later when you need to kill the process...
Process pToKill = Process.GetProcessById(someVariableICanGetToLater);
pToKill.Kill();


I assume that, since the batch file launches the Java app, knowing the id of the batch file process won't help.

Process.GetProcessesByName("java") gives you a list of all the java.exe instances.


pseudo code . Fill in OS specific calls here:

int cs_pid=getProcessByName("CSharpAppName");
int javaPid[]= getProcessByName("JavaAppName");

bool javaToCSharpRelationship[]=new bool[javaPid.size];

int loopCounter=0;

forEach int in javaPid
begin
      if(isChildOf(cs_pid,java_pid))
      begin
         // OS call or Wrapper to determine relationship between c# pid
         // and Java app PID
         javaToCSharpRelationShip[loopCounter]=true;
         //can call kill here if you like
      end
      else 
         javaToCSharpRelationShip[loopCounter]=false;
     loopCounter++;
end

At the end of this code you should have an array of PID's which are children of the C# app.


Unless the batch file does lots of things, hard to accomplish using only .NET's Process and ProcessStartInfo classes, another option would be to start directly java.exe and control the arguments and environment variables via ProcessStartInfo's Arguments and EnvironmentVariables properties.

Then you could use Jason's approach to store java.exe process' id in order to kill it later.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号