C # Process.kill () solution for access Denied denied

Source: Internet
Author: User

Requirements: many times we need to run a few console in the background to calculate the data, then after the deployment to the client server, if there is a sudden exception, the program hangs off, that ...?

Solution: encapsulated a background to run the program constantly monitoring the function, if the discovery program is abnormal, but the process is still in, this time kill the process, restart the background calculation program, where the calculation program is "console running program."

The code is as follows:

   if(whether the process is hung off)                {                    //Get all ProcessesProcess[] PS =process.getprocesses ();  for(inti =0; I < PS. Length; i++)                    {                        if(Ps[i]. Processname.startswith ("ProcessName")) {//Statistics crash CountCrashCount (obj.                            Name); Ps[i].                        Kill (); }} process.start (Path);Console.WriteLine (ProcessName+"The program has been restarted! ");                }

Debugging:

Exceptions can be seen in the Kill () process when the "Access Denied", on the Internet search, the solution is roughly the following:

    1. Add identity to Config
      • <system.web>
        <identity impersonate= "true" Username= "Administrator" password= "123456"/>
        </system.web>
    2. The test program runs as "Administrator"
    3. Assigning permissions to a monitored program directory

As a result, none of these approaches solves this problem.

I looked at the comment for the Kill () method:

 //        //Summary://immediately stops the associated process. //        //Exception://System.ComponentModel.Win32Exception://failed to terminate the associated process. -or-is terminating the process.        -or-The associated process is a Win16 executable file. //        //System.NotSupportedException://you are trying to call System.Diagnostics.Process.Kill () for a process that is running on a remote computer.        This method is available only for processes that are running on the local computer. //        //System.InvalidOperationException://the process has exited. -or-there is no process associated with this System.Diagnostics.Process object.          Public voidKill ();

Found to be a win32exception exception, and then I looked up the MS official documents, and sure enough to find:

It probably means that if the monitoring program is console, this kind of writing is OK and can end the process normally. But because of the need to show some monitoring data on the interface here, I'm using WPF, which is the image interface program in the document.

The exact words of MS are this: if you call Kill, you may lose data edited by the process or resources assigned to the process. Kill causes the process to terminate abnormally, so it should only be used if necessary. CloseMainWindow enables the process to terminate and close all windows in an orderly fashion, so it is better to use it for applications with interfaces. If CloseMainWindow fails, you can use kill to terminate the process. Kill is the only way to terminate a process that does not have a graphical interface.

Change the Kill method () to CloseMainWindow () to kill the process normally.

The above is a two-day encounter of an anomaly, let me summarize a new conclusion, in the face of problems, do not blindly go to Google or Baidu, should first consult MS official documents.

C # Process.kill () solution for access Denied denied

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.