How to get processID from Process.start ()

I have the following code

log.Info("Starting jar");
System.Diagnostics.ProcessStartInfo si = new ProcessStartInfo(procName);
si.RedirectStandardOutput = true;
si.RedirectStandardError = true;
si.UseShellExecute = false;
si.CreateNoWindow = false;
si.WorkingDirectory = System.IO.Directory.GetParent(Application.ExecutablePath) + "\\" + Properties.Settings.Default.rootDirectory;

//start a new process for Client
Process process = new Process();
process.StartInfo = si;
process.Start();
String name = process.ProcessName;
javaClientProcessId = process.Handle;
int javaProcessID = process.Id;

using this code, I get cmd as the name of the process where in taskManager it appears as java.exe . From the code it gives 5412 as the process.idand 1029 both process.Handle, where 6424 - is the identifier of the process java.exe
Is there any other method, from which I can get the same process ID , which is in the TaskManager

Note procName is the path to the Bat file in which it runs the jar file.

EDITED

When I execute the following code, it gives an error in the line process.Kill().

if (process != null)
{
     process.Kill();
     process.Close();
     process.Dispose();
}

, (6504)

+4
3

try
{
    Process[] javaProcList = Process.GetProcessesByName("java");
    foreach (Process javaProc in javaProcList)
    {
        javaProc.Kill();
        javaProc.Close();
        javaProc.Dispose();

        Console.WriteLine("StopJar -Java Process Stopped ");
        log.Debug("StopJar -Java Process Stopped ");
     }
 }
 catch (Exception exp)
 {
     log.Error("StopJar - Unable to kill Java Process", exp);
     Console.WriteLine("Error while closing: " + exp.Message);
  }
+2

. parentProcess - cmd.exe. .

using System.Diagnostics;

System.Runtime.InteropServices;

class Program {

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern IntPtr CreateToolhelp32Snapshot(uint dwFlags, uint th32ProcessID);

    [DllImport("kernel32.dll")]
    private static extern bool Process32First(IntPtr hSnapshot, ref PROCESSENTRY32 lppe);

    [DllImport("kernel32.dll")]
    private static extern bool Process32Next(IntPtr hSnapshot, ref PROCESSENTRY32 lppe);

    [StructLayout(LayoutKind.Sequential)]
    private struct PROCESSENTRY32 {
        public uint dwSize;
        public uint cntUsage;
        public uint th32ProcessID;
        public IntPtr th32DefaultHeapID;
        public uint th32ModuleID;
        public uint cntThreads;
        public uint th32ParentProcessID;
        public int pcPriClassBase;
        public uint dwFlags;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
        public string szExeFile;
    }

    public static class helper {
        public static Process[] getChildProcesses(int parentProcessID) {
            var ret = new List<Process>();
            uint TH32CS_SNAPPROCESS = 2;

            IntPtr hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
            if (hSnapshot == IntPtr.Zero) {
                return ret.ToArray();
            }
            PROCESSENTRY32 procInfo = new PROCESSENTRY32();
            procInfo.dwSize = (uint)Marshal.SizeOf(typeof(PROCESSENTRY32));
            if (Process32First(hSnapshot, ref procInfo) == false) {
                return ret.ToArray();
            }
            do {
                if ((int)procInfo.th32ParentProcessID == parentProcessID) {
                    ret.Add(Process.GetProcessById((int)procInfo.th32ProcessID));
                }
            }
            while (Process32Next(hSnapshot, ref procInfo));

            return ret.ToArray();
        }
        public static void killChildProcesses(int parentProcessID) {
            foreach (var p in getChildProcesses(parentProcessID))
                p.Kill();
        }
    }
0

1) java bat

Process p = new Process();  
ProcessStartInfo si = new ProcessStartInfo();  
si.Arguments = @"-jar app.jar";  
si.FileName = "java";  
p.StartInfo = si;  
p.Start();  
Console.WriteLine(p.Id);  
if (!p.HasExited) 
   p.Kill();  

, try..catch, WaitForExit() HasExited , , " " .

, " " , ?

2) If you must use the bat file, then get the KillProcessAndChildren () method from programmatically process the process tree in C # (requires a link to System.Management) and call

KillProcessAndChildren(javaProcessID); 

He will kill the main process and all his children.

3) And, of course, you can still use your source code and list all processes by name

Process[] pp = Process.GetProcessesByName("java");
foreach (Process p in pp) 
    p.Kill();

pp = Process.GetProcessesByName("cmd");
foreach (Process p in pp)
    p.Kill();

but if you have several java / cmd processes, this will delete all of them, which may not be very good.

0
source

Source: https://habr.com/ru/post/1538985/


All Articles