Andrew
Andrew

Reputation: 9

How to kill multiple processes

I need some help, Within a program I am creating! I have a simple task manager where i can kill a single process! However, i need it so I can kill multiple processes, been stuck a few days and a little unsure how to kill multiple processes. Can anyone give me any advice?

By multiple processes, i dont mean just put more processes in along side firefox, i mean load multiple processes from a listview or sql?

here is my code so far. I was thinking maybe it was possible to save the processes to sql, and then load them in to where fire fox is?

foreach (System.Diagnostics.Process pr in System.Diagnostics.Process.GetProcesses())//GETS PROCESSES
{
  if (pr.ProcessName == "firefox")//KILLS FIREFOX.....REMOVE FIREFOX.....CONNECT SAVED SQL PROCESSES IN HERE MAYBE??
  {
      pr.Kill(); //KILLS THE PROCESSES
  }
}

Upvotes: 0

Views: 2931

Answers (1)

Damith
Damith

Reputation: 63065

DataSet ds = new DataSet();// SQL STUFF
SqlConnection con = new SqlConnection(ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("SELECT ProcessName FROM ApplicationProcesses", con);
// since you start from SqlDataAdapter I'm continue from there..           
da.Fill(ds, "ProcessNames");
// get the process in the database to a array
string[] preocesArray = ds.Tables["ProcessNames"]
        .AsEnumerable()
        .Select(row => row.Field<string>("ProcessName"))
        .ToArray();

// kill the process if it is in the list 
var runningProceses = System.Diagnostics.Process.GetProcesses();
for (int i = 0; i < runningProceses.Length; i++)
{
    if (preocesArray.Contains(runningProceses[i].ProcessName))
    {
        runningProceses[i].Kill(); //KILLS THE PROCESSES
    }
}

Upvotes: 3

Related Questions