You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current implementation for getting the parent id of the current process if extremely slow:
The parent Id is not even cached, but is retrieved over and over again. On several systems I see module load times of 5+ seconds, where almost all of the time is spent blocking on the wmi call, waiting for WmiPrvSe to enumerate all processes.
The Id of the process that created us will not change, so cache it and/or use a more efficient method of getting the parent id.
The check for process start time would still have to be done (not in the example).
/// suggested alternative (about 100 times faster)publicstaticclassProcessInfoUtil{publicstaticSystem.Diagnostics.ProcessGetParentProcess(){returnParentProcessId==0?null:System.Diagnostics.Process.GetProcessById(ParentProcessId);}publicstaticreadonlyintParentProcessId=GetParentProcessId();privatestaticintGetParentProcessId(){varpi=newPROCESS_BASIC_INFORMATION();intactual;if(0==NativeMethods.NtQueryInformationProcess(newIntPtr(-1),0/*processbasicInformation*/,refpi,pi.Size,outactual)){return(int)pi.InheritedFromUniqueProcessId;}else{return0;}}[StructLayout(LayoutKind.Sequential,Pack=1)]privatestructPROCESS_BASIC_INFORMATION{publicIntPtrExitStatus;publicIntPtrPebBaseAddress;publicIntPtrAffinityMask;publicIntPtrBasePriority;publicUIntPtrUniqueProcessId;publicIntPtrInheritedFromUniqueProcessId;publicintSize{get{returnMarshal.SizeOf(typeof(PROCESS_BASIC_INFORMATION));}}}staticclassNativeMethods{[DllImport("NtDll",SetLastError=true)]publicstaticexternintNtQueryInformationProcess(IntPtrProcessHandle,intprocessInformationClass,refPROCESS_BASIC_INFORMATIONProcessInformation,intprocessInformationLength,outintreturnLength);}}
/// this is the current implementationstringwmiQuery=String.Format(CultureInfo.CurrentCulture,"Select * From Win32_Process Where Handle='{0}'",current.Id);using(CimSessioncimSession=CimSession.Create(null)){IEnumerable<CimInstance>processCollection=cimSession.QueryInstances("root/cimv2","WQL",wmiQuery);intparentPid=processCollection.Select(
cimProcess =>Convert.ToInt32(cimProcess.CimInstanceProperties["ParentProcessId"].Value,CultureInfo.CurrentCulture)).FirstOrDefault();if(parentPid==0)returnnull;
...
The current implementation for getting the parent id of the current process if extremely slow:
The parent Id is not even cached, but is retrieved over and over again. On several systems I see module load times of 5+ seconds, where almost all of the time is spent blocking on the wmi call, waiting for WmiPrvSe to enumerate all processes.
The Id of the process that created us will not change, so cache it and/or use a more efficient method of getting the parent id.
The check for process start time would still have to be done (not in the example).