arrays - How to kill a list of processes in c# -


i new c# , trying kill list of processes. able use code below kill 1 process change kills list of processes. on time list grow ideally find way updating list quick , easy.

try {     foreach (process proc in process.getprocessesbyname("notepad"))     {         proc.kill();     } } catch (exception) {     console.writeline("procces not found."); } 

i'm sorry if have overlooked question asked this. thank in advance provided.

the observablecollection<t> class provides event if collection changed. can create collection of process , handle new added items in event handler:

observablecollection<process> processes = new observablecollection<process>(); processes.collectionchanged += processes_collectionchanged;  static void processes_collectionchanged(object sender, notifycollectionchangedeventargs e) {     if (e.newitems != null)         foreach (var process in e.newitems.oftype<process>())             process.kill();     processes.clear();//remove processes list } 

but not worth in way. think more easier call code every time want kill process.

the way collection worthwhile if change generic argument string or int , pass process name or pid. in cases, can determine process object in collectionchanged event handler , kill them there. (like in code.)


Popular posts from this blog