2013-05-20 81 views
0

我需要一些幫助,在我正在創建的程序中!我有一個簡單的任務管理器,我可以殺死一個進程!但是,我需要它,所以我可以殺死多個進程,停留了幾天,並且有點不確定如何殺死多個進程。任何人都可以給我任何建議嗎?通過多個進程,我不是說只是把更多的進程放在一邊firefox,我的意思是加載多個進程從一個列表視圖或SQL?如何殺死多個進程

這是我的代碼到目前爲止。我想也許有可能將進程保存到SQL,然後將它們加載到火狐的位置?

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 
    } 
} 
+0

不知道你是問什麼....這個問題如何得到的進程名或如何殺死許多進程? – Mzf

+1

複製? http://stackoverflow.com/questions/10252155/killing-multiple-processes-that-were-started-from-nested-threads-c-sharp – Freelancer

+0

你好,我可以殺死一個進程,「把任何進程放在firefox是「,但這對我想要做的事沒有效率。我希望能夠將多個進程加載到firefox所在的位置。例如: 在我的任務管理器上,所有的進程加載,我選擇說10個進程殺死!所以我將這些進程保存到SQL中,是否有可能加載這些進程,所以我可以一次殺掉它們? – Andrew

回答

2
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 
    } 
} 
+0

也可以使用LINQ作爲第二部分: 'List processes = Process.GetProcesses()。其中​​(p => processNamesToKill.Contains(p.ProcessName))。ToList(); processes.ForEach(p => p.Kill());' –

+2

@ByteBlast我沒有使用linq或Foreach,因爲它可能會失敗,因爲我們在循環中執行kill過程,迭代器可能無法正常工作 – Damith

+0

認爲我然後呢!在da.Fill(ds,「ProcessNames」)上找出錯誤; 錯誤顯示「連接字符串屬性尚未初始化」 要仔細檢查一些代碼。有任何想法嗎? 謝謝你讓我走上正軌!非常感謝 – Andrew