Requirements: Many times we need to run a few console in the background to constantly calculate the data, then deployed to the client server, if there is a sudden exception, the program hangs off, that ...?
Solution: Encapsulated a background running program constantly monitor the function, if the Discovery program has an exception, but the process is still in, this time kill the process, restart the background calculation program, here the Calculation program is "console run program." The code is as follows: if (process is off) {& nbsp //Get all Processes & nbsp process[] ps = process.getprocesses (); for (int i = 0; i < PS. Length; i++) { &NBSP ; if (ps[i]. Processname.startswith ("ProcessName")) { //statistical crash count CrashCount (obj. Name); Ps[i]. Kill (); } &N Bsp } Process.Start (Pa) TH); Console.WriteLine (ProcessName + "program has been restarted!) "); } anomalies can be seen, Kill () Process when the "Access Denied", on the Internet search, the solution roughly on these kinds of: in config add identity<system.web> <identity impersonate= "true" Username= "Administrator" password= "123456"/></system.web> detection program to assign permissions to the monitored program directory by using "Run as Administrator" The result is that none of these methods resolves this issue. I reviewed the comment for the Kill () method: // //summary: // immediately stop the associated process.   //exception: // SYSTEM.COMPONENTMODEL.WIN32EXCEPTION:&NBS P // failed to terminate the associated process. -or-is terminating the process. -or-The associated process is a Win16 executable file. // // system.notsupportedexception: &NBS P 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 void Kill (); found to be a win32exception exception, and then I checked the official MS document, and there was a discovery: 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. ms's exact words 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 endThe only way to stop a process that does not have a graphical interface. Change the Kill method () to CloseMainWindow () to kill the process normally. 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.
(reprint) C # Process.kill () a solution to access Denied () denied ()