Search code examples
c#winformsprocesssystem.diagnostics

Process ID of process Started by Process Start


I am trying to kill the process by process ID which I am saving when the process start. But the process ID which I am capturing doesn't exists when I try to kill the process from code behind.

This is the code below to start the process and capture the process ID.

private List<int> pids = new List<int>();
public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pids.Clear();
            Process myprocess= new Process();

            myprocess.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\cmdkey.exe");
            myprocess.StartInfo.Arguments = "C:\\rdp\\RemoteIn.rdp";
            myprocess.Start();
            pids.Add(myprocess.Id);          
        }

        private void terminateAll()
        {
            //foreach (var p in pids) p.Kill();

            foreach (var i in pids)
            {
                Process p = Process.GetProcessById(i);
                p.Kill();

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            terminateAll();
        }

When I click button to terminate the process it shows following error.

enter image description here

Is there any way to fix this.

After using Palani Kumar code, I am getting below exception. enter image description here

Form Looks like this

enter image description here


Solution

  • I don't know why you are declared pids as List<int> and cleared the list (pids.Clear();) on button click event. Anyway the below will work for creating multiple processes also.

    EDIT: As so far discussed with Amrit. The Windows 8 creating sub processes for mstsc with connecting same domain. So I slightly modified my code.

        private void button1_Click(object sender, EventArgs e)
        {
            //pids.Clear();
            Process myprocess = new Process();
    
            myprocess.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\syswow64\mstsc.exe");
            myprocess.StartInfo.Arguments = "C:\\rdp\\RemoteIn.rdp";
            myprocess.Start();
            Thread.Sleep(100);
            /* Edit */
            Process proc = Process.GetProcessesByName("mstsc").FirstOrDefault(x => !pids.Any(y => y == x.Id));
            if(proc != null)
            {
               pids.Add(proc.Id);
            }
        }
    

    And

        private void terminateAll()
        {
            foreach (var i in pids)
            {
                try
                {
                    Process p = Process.GetProcessById(i);
                    p.Kill();
                }
                catch (Exception ex)
                {
                    //throw exception if we close the mstsc manually
                }
            }
        }