C # Process. Kill () Access Denied Solution

Source: Internet
Author: User

C # Process. Kill () Access Denied Solution
Requirement: in many cases, we need to run several consoles in the background to continuously compute data. If an unexpected exception occurs after the application is deployed on the client server, the program fails ...? Solution: encapsulate a function that constantly monitors programs running in the background. if an exception is found in the program but the process is still running, Kill the process and restart the background computing program, all the computing programs here are "console running programs ". The Code is as follows: if (whether the Process has crashed) {// obtain all processes Process [] ps = Process. getProcesses (); for (int I = 0; I <ps. length; I ++) {if (ps [I]. processName. startsWith ("ProcessName") {// counts the crash count CrashCount (obj. name); ps [I]. kill () ;}} Process. start (Path); Console. writeLine (ProcessName + "the program has been restarted! ");} In the exception, we can see that" access is denied "occurs during the Kill () process. After searching the Internet, the solution is roughly like this: add identity <system. web> <identity impersonate = "true" userName = "Administrator" password = "123456"/> </system. the web> detection program uses "Run as administrator" to assign permissions to the Monitored Program directory. As a result, none of these methods can solve this problem. I have checked the Note of the Kill () method: // Summary: // stop the associated process immediately. //// Exception: // System. ComponentModel. Win32Exception: // The associated process cannot be terminated. -Or-the process is being terminated. -Or-the associated process is a Win16 executable file. /// System. NotSupportedException: // you are trying to call System. Diagnostics. Process. Kill () for a Process running on a remote computer (). This method is only available for processes running on the local computer. /// System. InvalidOperationException: // The process has exited. -Or-there is no Process associated with this System. Diagnostics. Process object. Public void Kill (); found a Win32Exception exception, And then I checked the official ms documentation, and found that: it probably means that if this monitoring program is a Console, there is no problem with this writing, and the process can be terminated normally. However, some monitoring data needs to be displayed on the interface. Here I use WPF, which is the image interface program described in the document. The original statement of MS is as follows: if Kill is called, data edited by the process may be lost or resources allocated to the process. Kill causes abnormal Process Termination and should be used only when necessary. CloseMainWindow allows the process to terminate and close all windows in an orderly manner, so it is better to use it for interface applications. If CloseMainWindow fails, Kill the process. Kill is the only way to terminate a process without a graphical interface. Change the Kill method () to CloseMainWindow () to Kill the process. The above is an exception encountered in the past two days. I have come up with a new conclusion. When encountering problems, do not blindly go to google or Baidu, check the official ms documentation first.

Related Article

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.