Kill processes that occupy CPU for a long time

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.