-2

It is a small part of my program where my program needs to start the program from DataGridView (Content Clicked Event) and its just executing that program perfectly but is not able to close it because some of incoming programs don't have same process name as file name. I tried getting processid too but it throws following error (can you please provide me a working code because i can get the id of my winform process but how can i get the processid of externally launched app from my program. i tried it it throws the following error

An unhandled exception of type 'System.ArgumentException' occurred in System.dll Additional information: Process with an Id of 16924 is not running.)"

the code in which i am getting process id but fails is below

    private void button1_Click(object sender, EventArgs e)
    {
        var processid = Process.Start("Calc");
        pn =processid.ProcessName;
        pid = processid.Id;
    }
    int pid;
    String pn;
    private void button2_Click(object sender, EventArgs e)
    {
           var process1 = Process.GetProcessById(pid);
            process1.Kill();
    }

The dummy code is below.

I have already tried:

    private void button1_Click(object sender, EventArgs e)
    {
        Process.Start("Calc");
    }
    private void button2_Click(object sender, EventArgs e)
    {
        foreach (var process in Process.GetProcessesByName("Calc"))
        {
            process.Kill();
        }
    }

My Code:

    private void button1_Click(object sender, EventArgs e)
    {
        Process.Start("THIS PATH WILL COME FROM DATABASE");
    }
    private void button2_Click(object sender, EventArgs e)
    {
        foreach (var process in Process.GetProcessesByName("PROCESS NAME WHICH MY PROGRAM STARTED"))
        {
            process.Kill();
        }
    }
James Z
  • 12,209
  • 10
  • 24
  • 44
Rohaan Alam
  • 5
  • 1
  • 6

1 Answers1

2

When you start the process get the Id of the process, and store it. You can then get the process by id to kill it. This not only ensures you don't need to know the name of the process (in case it's different from the path to start it) but ensures that you kill the correct instance if there are multiple instances running, some of which weren't started by your program.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • Would you store this in individual variables or would it be best to store it in a `list` and iterate through the `list`? – Jaskier Jan 02 '19 at 21:18
  • 1
    @Symon That depends on the nature of your program and what it needs to actually do. Are you intending to start multiple instances and then kill them all? Are you only intending to have one running at a time? Is there a single button to kill all running instances, or different buttons for different instances? What you want to do will affect how you want to store the data. – Servy Jan 02 '19 at 21:23
  • Are the other programs windowed apps (like Windows Forms). If so, you may want to be more polite when you kill them. Send them a `WM_QUIT` message, then, if they don't kill themselves in some amount of time, get out the `process.Kill` shotgun. To do this, you'll need to find the top-level window associated with the process. – Flydog57 Jan 02 '19 at 22:28
  • @Symon Actually In Real Concept The WINFORM Needs To close All Applications When It Is Being Disposed. So In My Practice or Dummy Code I want My Button To Close All Started Applications. (BTW Thanks You All I Believe I will Endup Bieng Happy ;) – Rohaan Alam Jan 03 '19 at 06:25
  • @Flydog57 They Could Be Or Coukd Not Be Actually User WIll Add Programs And Execute them Accodingly... – Rohaan Alam Jan 03 '19 at 06:26
  • @Servy Really Thanks Brother But I am Making Mistake Somewhere Please Check The Question Again I have Edited It :) – Rohaan Alam Jan 03 '19 at 06:28