I have the following section of code designed to count the number of Excel processes currently in progress:
Func<int> OpenExcelProcessesCount =
() => System.Diagnostics.Process.GetProcessesByName("Excel")
.Where(p => !p.HasExited)
.Count();
Then I get the score at different points with the code, for example the following:
int excelAppCount = OpenExcelProcessesCount();
This code works 100% for several months. Then suddenly, today, he constantly gives me an exception, which reads as follows:
Exception: ApplicationThreadException
Message: Access is denied
Stack Trace:
at System.Diagnostics.ProcessManager.OpenProcess(Int32
processId, Int32 access, Boolean throwIfExited)
at System.Diagnostics.Process.GetProcessHandle(Int32
access, Boolean throwIfExited)
at System.Diagnostics.Process.get_HasExited()
etc...
, Process.HasExited ( System.Diagnostics.Process.get_HasExited() , ) . " " , , Excel , . .NET- .
, , System.Diagnostics.ProcessManager.OpenProcess(Int32 processId, Int32 access, Boolean throwIfExited). , "true" "throwIfExited". , , Process.HasExited try-catch , , HasExited "". ?
, , "Access is denied". - , , , , ?
, Stack Overflow, : hasExited throw 'System.ComponentModel.Win32Exception?. :
" runas, SYNCHRONIZE , PROCESS_QUERY_INFORMATION , GetExitCodeProcess , hasEnded Win32 ".
, , , . - , , , , , , - , . ( Excel, .)
...
Update:
, , . , , , "" . , .
, - . , ROT , / Excel, , "" . , , .
, , .