Kill processes that occupy CPU for a long time
Recently, the project needs to call an external tool through cmd to convert and process files. However, due to the conversion tool, it occupies too many CPU resources for a long time, seriously affecting system performance.
The code for killing a process is as follows:
Class Program <br/>{< br/> static void main (string [] ARGs) <br/>{< br/> console. writeline ("Name of the process that killed the timeout:"); <br/> while (true) <br/>{< br/> thread. sleep (5000); <br/> system. diagnostics. process [] PS = system. diagnostics. process. getprocessesbyname ("process name"); </P> <p> foreach (Process in PS) <br/>{< br/> timespan Ts = process. totalprocessortime; <br/> If (TS. totalseconds> 5) <br/> {<br/> console. writeline (system. datetime. now. tostring () + "kill a timeout worker 2swf Process"); <br/> console. writeline ("filename:" + process. startinfo. filename + ", arguments:" + process. startinfo. arguments); <br/> process. kill (); <br/>}< br/>}
The killed timeout process can be recorded in files and memory for subsequent processing.