locked
Identify and Kill Process RRS feed

  • Question

  • User-1136712524 posted

    Hi;

    Let's say i create 3 different process like this:

    For i = 1 To 3
         Dim pr As New Process
         pr.StartInfo.UseShellExecute = True
         pr.StartInfo.Arguments = "c:\program\info" & i.ToString & ".ini"
         pr.StartInfo.FileName = "c:\program\myprogram.exe"
         pr.Start()
    Next

    Now, if i open the Task Manager, i'll see that all 3 processes are running with the same "Image Name": myprogram.exe
    And here is where i'm getting stuck:

    Lets say "from another application", at some time of the day, i need to kill the process which was started with the parameter "info2.ini", which was the second process created, how can i retrieve that one it if all three has the same name?

    Thanks in advance!

    Friday, December 21, 2007 9:34 PM

Answers

  • User-1091210821 posted

    Hi;

    Now, if i open the Task Manager, i'll see that all 3 processes are running with the same "Image Name": myprogram.exe
    And here is where i'm getting stuck:

    Lets say "from another application", at some time of the day, i need to kill the process which was started with the parameter "info2.ini", which was the second process created, how can i retrieve that one it if all three has the same name?

    Thanks in advance!

    The very simple way to identify task in task manager, is right click and then click go to process, it will take you directly to that process, where you can terminate it. You also have pid tab, you just have to enable it from the top

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 24, 2007 9:52 PM

All replies

  • User-1363023537 posted

    Hay emerbrito,
     You can Identify this processes using Process Id you can see in your task manager their PID or process ID is different.

    Satalaj. 

    Saturday, December 22, 2007 1:18 AM
  • User-1136712524 posted

    I don't see a way to use the process id to acomplish this, because i don't know the ID when i'm looking for the process to kill it.
    Let's go through the hole thing again:

    One aplication start the 3 processes (as in the first post) and after that, the aplication is closed (but the processes stays running).
    Now, when i open another application, i can get the 3 processes with this code: 

    For Each p In System.Diagnostics.Process.GetProcessesByName("myprogram")
       ' if "p" is waht i'm looking for, kill it with p.kill
    Next

    And here is where things get complicated, all processes has the same name: "myprogram".
    They do have different ID's but i can't tell wich one started the "info2.ini" (as a parameter) based on it's ID.
    What i need here is a way to tell wich one started which "ini file", so, i know wich one to kill.

    The hole thing would be much easyer if i could give the process an custom name or custom ID at the time i created it.
    If i could start it as "MyProcess1", "MyProcess2"... at any time i need to identify it in another applications i could just look for "MyProcess2".

    Saturday, December 22, 2007 2:29 AM
  • User-1363023537 posted
    Thats good Idea
    Monday, December 24, 2007 2:12 AM
  • User-1091210821 posted

    Hi;

    Now, if i open the Task Manager, i'll see that all 3 processes are running with the same "Image Name": myprogram.exe
    And here is where i'm getting stuck:

    Lets say "from another application", at some time of the day, i need to kill the process which was started with the parameter "info2.ini", which was the second process created, how can i retrieve that one it if all three has the same name?

    Thanks in advance!

    The very simple way to identify task in task manager, is right click and then click go to process, it will take you directly to that process, where you can terminate it. You also have pid tab, you just have to enable it from the top

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 24, 2007 9:52 PM
  • User1356714375 posted
    Dim myProcesses As Process() = Process.GetProcessesByName("myprogram")
    Dim myProcess As Process
    For Each myProcess In myProcesses
    If myProcess.MainWindowTitle = "" Then myProcess.Kill()
    Next myProcess
    Tuesday, January 15, 2008 9:10 PM
  • User-1136712524 posted

    Dim myProcesses As Process() = Process.GetProcessesByName("myprogram")
    Dim myProcess As Process
    For Each myProcess In myProcesses
    If myProcess.MainWindowTitle = "" Then myProcess.Kill()
    Next myProcess

    Thanks DaveCS,

    What if there's no windows? All the process are running on the background (which is my case)? 
    I'll still have to found a way to identify each one.

    Wednesday, January 16, 2008 12:57 PM
  • User1356714375 posted

     Try this. Should work for you.

     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Process.Start("notepad.exe")
            Process.Start("notepad.exe")
            Process.Start("notepad.exe")
            Dim p As Process() = Process.GetProcessesByName("notepad")
            If p.Length > 0 Then
                For i As Integer = 0 To p.Length - 1
                    MessageBox.Show(p(i).Id)
                Next
            End If
        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim myProcesses As Process() = Process.GetProcessesByName("notepad")
            Dim myProcess As Process
            For Each myProcess In myProcesses
                myProcess.Kill()
            Next myProcess
        End Sub

    Wednesday, January 16, 2008 10:16 PM
  • User-398999063 posted

    Code Is in C# , But May be give you some Idea. it Will kill the Process as it is initialized

     

    public Cleanit()
            {
                InitializeComponent();

                WqlEventQuery q = new WqlEventQuery("Win32_ProcessStartTrace");
                using (ManagementEventWatcher w = new ManagementEventWatcher(q))
                {
                    w.EventArrived += new EventArrivedEventHandler(ProcessStartEventArrived);
                    w.Start();
                  
                }
               
            }


            static void ProcessStartEventArrived(object sender, EventArrivedEventArgs e)
            {
               
                foreach (PropertyData pd in e.NewEvent.Properties)
                {
                    if (pd.Value.ToString() == "myapplication.exe")
                    {

                        Process[] aProc = Process.GetProcessesByName("myApplication");
                        if (aProc.Length > 0)
                            aProc[0].Kill();
                        break;
                    }
                   
                }
            }

    Friday, July 9, 2010 6:38 AM